#include "crc.h"

/******************************************************************************
 * Name:    CRC-4/ITU           x4+x+1
 * Poly:    0x03
 * Init:    0x00
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc4ITU_table[256] = {
0x00, 0x07, 0x0E, 0x09, 0x05, 0x02, 0x0B, 0x0C, 0x0A, 0x0D, 0x04, 0x03, 0x0F, 0x08, 0x01, 0x06,
0x0D, 0x0A, 0x03, 0x04, 0x08, 0x0F, 0x06, 0x01, 0x07, 0x00, 0x09, 0x0E, 0x02, 0x05, 0x0C, 0x0B,
0x03, 0x04, 0x0D, 0x0A, 0x06, 0x01, 0x08, 0x0F, 0x09, 0x0E, 0x07, 0x00, 0x0C, 0x0B, 0x02, 0x05,
0x0E, 0x09, 0x00, 0x07, 0x0B, 0x0C, 0x05, 0x02, 0x04, 0x03, 0x0A, 0x0D, 0x01, 0x06, 0x0F, 0x08,
0x06, 0x01, 0x08, 0x0F, 0x03, 0x04, 0x0D, 0x0A, 0x0C, 0x0B, 0x02, 0x05, 0x09, 0x0E, 0x07, 0x00,
0x0B, 0x0C, 0x05, 0x02, 0x0E, 0x09, 0x00, 0x07, 0x01, 0x06, 0x0F, 0x08, 0x04, 0x03, 0x0A, 0x0D,
0x05, 0x02, 0x0B, 0x0C, 0x00, 0x07, 0x0E, 0x09, 0x0F, 0x08, 0x01, 0x06, 0x0A, 0x0D, 0x04, 0x03,
0x08, 0x0F, 0x06, 0x01, 0x0D, 0x0A, 0x03, 0x04, 0x02, 0x05, 0x0C, 0x0B, 0x07, 0x00, 0x09, 0x0E,
0x0C, 0x0B, 0x02, 0x05, 0x09, 0x0E, 0x07, 0x00, 0x06, 0x01, 0x08, 0x0F, 0x03, 0x04, 0x0D, 0x0A,
0x01, 0x06, 0x0F, 0x08, 0x04, 0x03, 0x0A, 0x0D, 0x0B, 0x0C, 0x05, 0x02, 0x0E, 0x09, 0x00, 0x07,
0x0F, 0x08, 0x01, 0x06, 0x0A, 0x0D, 0x04, 0x03, 0x05, 0x02, 0x0B, 0x0C, 0x00, 0x07, 0x0E, 0x09,
0x02, 0x05, 0x0C, 0x0B, 0x07, 0x00, 0x09, 0x0E, 0x08, 0x0F, 0x06, 0x01, 0x0D, 0x0A, 0x03, 0x04,
0x0A, 0x0D, 0x04, 0x03, 0x0F, 0x08, 0x01, 0x06, 0x00, 0x07, 0x0E, 0x09, 0x05, 0x02, 0x0B, 0x0C,
0x07, 0x00, 0x09, 0x0E, 0x02, 0x05, 0x0C, 0x0B, 0x0D, 0x0A, 0x03, 0x04, 0x08, 0x0F, 0x06, 0x01,
0x09, 0x0E, 0x07, 0x00, 0x0C, 0x0B, 0x02, 0x05, 0x03, 0x04, 0x0D, 0x0A, 0x06, 0x01, 0x08, 0x0F,
0x04, 0x03, 0x0A, 0x0D, 0x01, 0x06, 0x0F, 0x08, 0x0E, 0x09, 0x00, 0x07, 0x0B, 0x0C, 0x05, 0x02,
};

void crc4_itu_GenerateTable()
{
    uint8_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x0C;        // 0x0C = (reverse 0x03)>>(8-4)
            else
                crc = (crc >> 1);
        }
        crc4ITU_table[i] = crc;
    }
//    /* 用于打印数组 */
//    printf("uint8_t crc4ITU_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc4ITU_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}


uint8_t crc4_itu_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc4ITU_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-4/ITU           x4+x+1
 * Note:    d for直接计算
 *****************************************************************************/
uint8_t crc4_itu_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        crc ^= *data++;                 // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x0C;// 0x0C = (reverse 0x03)>>(8-4)
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-5/EPC           x5+x3+1
 * Poly:    0x09
 * Init:    0x09
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc5EPU_table[256] = {
0x00, 0x48, 0x90, 0xD8, 0x68, 0x20, 0xF8, 0xB0, 0xD0, 0x98, 0x40, 0x08, 0xB8, 0xF0, 0x28, 0x60,
0xE8, 0xA0, 0x78, 0x30, 0x80, 0xC8, 0x10, 0x58, 0x38, 0x70, 0xA8, 0xE0, 0x50, 0x18, 0xC0, 0x88,
0x98, 0xD0, 0x08, 0x40, 0xF0, 0xB8, 0x60, 0x28, 0x48, 0x00, 0xD8, 0x90, 0x20, 0x68, 0xB0, 0xF8,
0x70, 0x38, 0xE0, 0xA8, 0x18, 0x50, 0x88, 0xC0, 0xA0, 0xE8, 0x30, 0x78, 0xC8, 0x80, 0x58, 0x10,
0x78, 0x30, 0xE8, 0xA0, 0x10, 0x58, 0x80, 0xC8, 0xA8, 0xE0, 0x38, 0x70, 0xC0, 0x88, 0x50, 0x18,
0x90, 0xD8, 0x00, 0x48, 0xF8, 0xB0, 0x68, 0x20, 0x40, 0x08, 0xD0, 0x98, 0x28, 0x60, 0xB8, 0xF0,
0xE0, 0xA8, 0x70, 0x38, 0x88, 0xC0, 0x18, 0x50, 0x30, 0x78, 0xA0, 0xE8, 0x58, 0x10, 0xC8, 0x80,
0x08, 0x40, 0x98, 0xD0, 0x60, 0x28, 0xF0, 0xB8, 0xD8, 0x90, 0x48, 0x00, 0xB0, 0xF8, 0x20, 0x68,
0xF0, 0xB8, 0x60, 0x28, 0x98, 0xD0, 0x08, 0x40, 0x20, 0x68, 0xB0, 0xF8, 0x48, 0x00, 0xD8, 0x90,
0x18, 0x50, 0x88, 0xC0, 0x70, 0x38, 0xE0, 0xA8, 0xC8, 0x80, 0x58, 0x10, 0xA0, 0xE8, 0x30, 0x78,
0x68, 0x20, 0xF8, 0xB0, 0x00, 0x48, 0x90, 0xD8, 0xB8, 0xF0, 0x28, 0x60, 0xD0, 0x98, 0x40, 0x08,
0x80, 0xC8, 0x10, 0x58, 0xE8, 0xA0, 0x78, 0x30, 0x50, 0x18, 0xC0, 0x88, 0x38, 0x70, 0xA8, 0xE0,
0x88, 0xC0, 0x18, 0x50, 0xE0, 0xA8, 0x70, 0x38, 0x58, 0x10, 0xC8, 0x80, 0x30, 0x78, 0xA0, 0xE8,
0x60, 0x28, 0xF0, 0xB8, 0x08, 0x40, 0x98, 0xD0, 0xB0, 0xF8, 0x20, 0x68, 0xD8, 0x90, 0x48, 0x00,
0x10, 0x58, 0x80, 0xC8, 0x78, 0x30, 0xE8, 0xA0, 0xC0, 0x88, 0x50, 0x18, 0xA8, 0xE0, 0x38, 0x70,
0xF8, 0xB0, 0x68, 0x20, 0x90, 0xD8, 0x00, 0x48, 0x28, 0x60, 0xB8, 0xF0, 0x40, 0x08, 0xD0, 0x98,
};

/* crc5_epu_GenerateTable */
void crc5_epu_GenerateTable()
{
    uint8_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;    // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x80)
                crc = (crc << 1) ^ 0x48;        // 0x0C = (reverse 0x03)>>(8-4)
            else
                crc = (crc << 1);
        }
        crc5EPU_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("uint8_t crc5EPU_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc5EPU_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint8_t crc5_epc_calc(uint8_t *data, uint32_t length)
{
    uint8_t crc = 0x48;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ (*data++);
        crc = crc5EPU_table[tmp];
    }
    return crc>>3;
}
/*
 * Name:    CRC-5/EPC           x5+x3+1
 * Note:    d for直接计算
 */
uint8_t crc5_epc_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0x48;        // Initial value: 0x48 = 0x09<<(8-5)
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for ( i = 0; i < 8; i++ )
        {
            if ( crc & 0x80 )
                crc = (crc << 1) ^ 0x48;        // 0x48 = 0x09<<(8-5)
            else
                crc <<= 1;
        }
    }
    return crc >> 3;
}
/******************************************************************************
 * Name:    CRC-5/ITU           x5+x4+x2+1
 * Poly:    0x15
 * Init:    0x00
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Note:
 *****************************************************************************/

static uint8_t crc5ITU_table[256] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x13, 0x14, 0x1D, 0x1A, 0x0F, 0x08, 0x01, 0x06,
0x0D, 0x0A, 0x03, 0x04, 0x11, 0x16, 0x1F, 0x18, 0x1E, 0x19, 0x10, 0x17, 0x02, 0x05, 0x0C, 0x0B,
0x1A, 0x1D, 0x14, 0x13, 0x06, 0x01, 0x08, 0x0F, 0x09, 0x0E, 0x07, 0x00, 0x15, 0x12, 0x1B, 0x1C,
0x17, 0x10, 0x19, 0x1E, 0x0B, 0x0C, 0x05, 0x02, 0x04, 0x03, 0x0A, 0x0D, 0x18, 0x1F, 0x16, 0x11,
0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x0C, 0x0B, 0x02, 0x05, 0x10, 0x17, 0x1E, 0x19,
0x12, 0x15, 0x1C, 0x1B, 0x0E, 0x09, 0x00, 0x07, 0x01, 0x06, 0x0F, 0x08, 0x1D, 0x1A, 0x13, 0x14,
0x05, 0x02, 0x0B, 0x0C, 0x19, 0x1E, 0x17, 0x10, 0x16, 0x11, 0x18, 0x1F, 0x0A, 0x0D, 0x04, 0x03,
0x08, 0x0F, 0x06, 0x01, 0x14, 0x13, 0x1A, 0x1D, 0x1B, 0x1C, 0x15, 0x12, 0x07, 0x00, 0x09, 0x0E,
0x15, 0x12, 0x1B, 0x1C, 0x09, 0x0E, 0x07, 0x00, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
0x18, 0x1F, 0x16, 0x11, 0x04, 0x03, 0x0A, 0x0D, 0x0B, 0x0C, 0x05, 0x02, 0x17, 0x10, 0x19, 0x1E,
0x0F, 0x08, 0x01, 0x06, 0x13, 0x14, 0x1D, 0x1A, 0x1C, 0x1B, 0x12, 0x15, 0x00, 0x07, 0x0E, 0x09,
0x02, 0x05, 0x0C, 0x0B, 0x1E, 0x19, 0x10, 0x17, 0x11, 0x16, 0x1F, 0x18, 0x0D, 0x0A, 0x03, 0x04,
0x0A, 0x0D, 0x04, 0x03, 0x16, 0x11, 0x18, 0x1F, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
0x07, 0x00, 0x09, 0x0E, 0x1B, 0x1C, 0x15, 0x12, 0x14, 0x13, 0x1A, 0x1D, 0x08, 0x0F, 0x06, 0x01,
0x10, 0x17, 0x1E, 0x19, 0x0C, 0x0B, 0x02, 0x05, 0x03, 0x04, 0x0D, 0x0A, 0x1F, 0x18, 0x11, 0x16,
0x1D, 0x1A, 0x13, 0x14, 0x01, 0x06, 0x0F, 0x08, 0x0E, 0x09, 0x00, 0x07, 0x12, 0x15, 0x1C, 0x1B,
};

void crc5_itu_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x15;        // 0x0C = (reverse 0x03)>>(8-4)
            else
                crc = (crc >> 1);
        }
        crc5ITU_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("uint8_t crc5ITU_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc5ITU_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}


uint8_t crc5_itu_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc5ITU_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-5/ITU           x5+x4+x2+1
 * Note:    d for直接计算
 *****************************************************************************/

uint8_t crc5_itu_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        crc ^= *data++;                 // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x15;// 0x15 = (reverse 0x15)>>(8-5)
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-5/USB           x5+x2+1
 * Poly:    0x05
 * Init:    0x1F
 * Refin:   True
 * Refout:  True
 * Xorout:  0x1F
 * Note:
 *****************************************************************************/
static uint8_t crc5USB_table[256] = {
0x00, 0x0E, 0x1C, 0x12, 0x11, 0x1F, 0x0D, 0x03, 0x0B, 0x05, 0x17, 0x19, 0x1A, 0x14, 0x06, 0x08,
0x16, 0x18, 0x0A, 0x04, 0x07, 0x09, 0x1B, 0x15, 0x1D, 0x13, 0x01, 0x0F, 0x0C, 0x02, 0x10, 0x1E,
0x05, 0x0B, 0x19, 0x17, 0x14, 0x1A, 0x08, 0x06, 0x0E, 0x00, 0x12, 0x1C, 0x1F, 0x11, 0x03, 0x0D,
0x13, 0x1D, 0x0F, 0x01, 0x02, 0x0C, 0x1E, 0x10, 0x18, 0x16, 0x04, 0x0A, 0x09, 0x07, 0x15, 0x1B,
0x0A, 0x04, 0x16, 0x18, 0x1B, 0x15, 0x07, 0x09, 0x01, 0x0F, 0x1D, 0x13, 0x10, 0x1E, 0x0C, 0x02,
0x1C, 0x12, 0x00, 0x0E, 0x0D, 0x03, 0x11, 0x1F, 0x17, 0x19, 0x0B, 0x05, 0x06, 0x08, 0x1A, 0x14,
0x0F, 0x01, 0x13, 0x1D, 0x1E, 0x10, 0x02, 0x0C, 0x04, 0x0A, 0x18, 0x16, 0x15, 0x1B, 0x09, 0x07,
0x19, 0x17, 0x05, 0x0B, 0x08, 0x06, 0x14, 0x1A, 0x12, 0x1C, 0x0E, 0x00, 0x03, 0x0D, 0x1F, 0x11,
0x14, 0x1A, 0x08, 0x06, 0x05, 0x0B, 0x19, 0x17, 0x1F, 0x11, 0x03, 0x0D, 0x0E, 0x00, 0x12, 0x1C,
0x02, 0x0C, 0x1E, 0x10, 0x13, 0x1D, 0x0F, 0x01, 0x09, 0x07, 0x15, 0x1B, 0x18, 0x16, 0x04, 0x0A,
0x11, 0x1F, 0x0D, 0x03, 0x00, 0x0E, 0x1C, 0x12, 0x1A, 0x14, 0x06, 0x08, 0x0B, 0x05, 0x17, 0x19,
0x07, 0x09, 0x1B, 0x15, 0x16, 0x18, 0x0A, 0x04, 0x0C, 0x02, 0x10, 0x1E, 0x1D, 0x13, 0x01, 0x0F,
0x1E, 0x10, 0x02, 0x0C, 0x0F, 0x01, 0x13, 0x1D, 0x15, 0x1B, 0x09, 0x07, 0x04, 0x0A, 0x18, 0x16,
0x08, 0x06, 0x14, 0x1A, 0x19, 0x17, 0x05, 0x0B, 0x03, 0x0D, 0x1F, 0x11, 0x12, 0x1C, 0x0E, 0x00,
0x1B, 0x15, 0x07, 0x09, 0x0A, 0x04, 0x16, 0x18, 0x10, 0x1E, 0x0C, 0x02, 0x01, 0x0F, 0x1D, 0x13,
0x0D, 0x03, 0x11, 0x1F, 0x1C, 0x12, 0x00, 0x0E, 0x06, 0x08, 0x1A, 0x14, 0x17, 0x19, 0x0B, 0x05,
};
void crc5_usb_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x14;// 0x14 = (reverse 0x05)>>(8-5)
            else
                crc = (crc >> 1);
        }
        crc5USB_table[i] = crc;
    }

///* 用于打印数组 */
//    printf("uint8_t crc5USB_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc5USB_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}
/* CRC5_usb_calc */
uint8_t crc5_usb_calc(uint8_t *data, uint32_t length)
{
    uint8_t crc = 0x1F;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ *data++;
        crc = crc5USB_table[tmp];
    }
    return crc^0x1F;
}

/*
 * Name:    CRC-5/USB           x5+x2+1
 * Note:    d for直接计算
 */
uint8_t crc5_usb_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0x1F;                // Initial value
    while(length--)
    {
        crc ^= *data++;                 // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x14;// 0x14 = (reverse 0x05)>>(8-5)
            else
                crc = (crc >> 1);
        }
    }
    return crc ^ 0x1F;
}
/******************************************************************************
 * Name:    CRC-6/ITU           x6+x+1
 * Poly:    0x03
 * Init:    0x00
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc6ITU_table[256] = {
0x00, 0x14, 0x28, 0x3C, 0x31, 0x25, 0x19, 0x0D, 0x03, 0x17, 0x2B, 0x3F, 0x32, 0x26, 0x1A, 0x0E,
0x06, 0x12, 0x2E, 0x3A, 0x37, 0x23, 0x1F, 0x0B, 0x05, 0x11, 0x2D, 0x39, 0x34, 0x20, 0x1C, 0x08,
0x0C, 0x18, 0x24, 0x30, 0x3D, 0x29, 0x15, 0x01, 0x0F, 0x1B, 0x27, 0x33, 0x3E, 0x2A, 0x16, 0x02,
0x0A, 0x1E, 0x22, 0x36, 0x3B, 0x2F, 0x13, 0x07, 0x09, 0x1D, 0x21, 0x35, 0x38, 0x2C, 0x10, 0x04,
0x18, 0x0C, 0x30, 0x24, 0x29, 0x3D, 0x01, 0x15, 0x1B, 0x0F, 0x33, 0x27, 0x2A, 0x3E, 0x02, 0x16,
0x1E, 0x0A, 0x36, 0x22, 0x2F, 0x3B, 0x07, 0x13, 0x1D, 0x09, 0x35, 0x21, 0x2C, 0x38, 0x04, 0x10,
0x14, 0x00, 0x3C, 0x28, 0x25, 0x31, 0x0D, 0x19, 0x17, 0x03, 0x3F, 0x2B, 0x26, 0x32, 0x0E, 0x1A,
0x12, 0x06, 0x3A, 0x2E, 0x23, 0x37, 0x0B, 0x1F, 0x11, 0x05, 0x39, 0x2D, 0x20, 0x34, 0x08, 0x1C,
0x30, 0x24, 0x18, 0x0C, 0x01, 0x15, 0x29, 0x3D, 0x33, 0x27, 0x1B, 0x0F, 0x02, 0x16, 0x2A, 0x3E,
0x36, 0x22, 0x1E, 0x0A, 0x07, 0x13, 0x2F, 0x3B, 0x35, 0x21, 0x1D, 0x09, 0x04, 0x10, 0x2C, 0x38,
0x3C, 0x28, 0x14, 0x00, 0x0D, 0x19, 0x25, 0x31, 0x3F, 0x2B, 0x17, 0x03, 0x0E, 0x1A, 0x26, 0x32,
0x3A, 0x2E, 0x12, 0x06, 0x0B, 0x1F, 0x23, 0x37, 0x39, 0x2D, 0x11, 0x05, 0x08, 0x1C, 0x20, 0x34,
0x28, 0x3C, 0x00, 0x14, 0x19, 0x0D, 0x31, 0x25, 0x2B, 0x3F, 0x03, 0x17, 0x1A, 0x0E, 0x32, 0x26,
0x2E, 0x3A, 0x06, 0x12, 0x1F, 0x0B, 0x37, 0x23, 0x2D, 0x39, 0x05, 0x11, 0x1C, 0x08, 0x34, 0x20,
0x24, 0x30, 0x0C, 0x18, 0x15, 0x01, 0x3D, 0x29, 0x27, 0x33, 0x0F, 0x1B, 0x16, 0x02, 0x3E, 0x2A,
0x22, 0x36, 0x0A, 0x1E, 0x13, 0x07, 0x3B, 0x2F, 0x21, 0x35, 0x09, 0x1D, 0x10, 0x04, 0x38, 0x2C,
};


void crc6_itu_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x30;// 0x30 = (reverse 0x03)>>(8-6)
            else
                crc = (crc >> 1);
        }
        crc6ITU_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("uint8_t crc6ITU_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc6ITU_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint8_t crc6_itu_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc6ITU_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-6/ITU           x6+x+1
 * Note:    d for直接计算
 *****************************************************************************/

uint8_t crc6_itu_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;         // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x30;// 0x30 = (reverse 0x03)>>(8-6)
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-7/MMC           x7+x3+1
 * Poly:    0x09
 * Init:    0x00
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Use:     MultiMediaCard,SD,ect.
 *****************************************************************************/
static uint8_t crc7MMC_table[256] = {
0x00, 0x12, 0x24, 0x36, 0x48, 0x5A, 0x6C, 0x7E, 0x90, 0x82, 0xB4, 0xA6, 0xD8, 0xCA, 0xFC, 0xEE,
0x32, 0x20, 0x16, 0x04, 0x7A, 0x68, 0x5E, 0x4C, 0xA2, 0xB0, 0x86, 0x94, 0xEA, 0xF8, 0xCE, 0xDC,
0x64, 0x76, 0x40, 0x52, 0x2C, 0x3E, 0x08, 0x1A, 0xF4, 0xE6, 0xD0, 0xC2, 0xBC, 0xAE, 0x98, 0x8A,
0x56, 0x44, 0x72, 0x60, 0x1E, 0x0C, 0x3A, 0x28, 0xC6, 0xD4, 0xE2, 0xF0, 0x8E, 0x9C, 0xAA, 0xB8,
0xC8, 0xDA, 0xEC, 0xFE, 0x80, 0x92, 0xA4, 0xB6, 0x58, 0x4A, 0x7C, 0x6E, 0x10, 0x02, 0x34, 0x26,
0xFA, 0xE8, 0xDE, 0xCC, 0xB2, 0xA0, 0x96, 0x84, 0x6A, 0x78, 0x4E, 0x5C, 0x22, 0x30, 0x06, 0x14,
0xAC, 0xBE, 0x88, 0x9A, 0xE4, 0xF6, 0xC0, 0xD2, 0x3C, 0x2E, 0x18, 0x0A, 0x74, 0x66, 0x50, 0x42,
0x9E, 0x8C, 0xBA, 0xA8, 0xD6, 0xC4, 0xF2, 0xE0, 0x0E, 0x1C, 0x2A, 0x38, 0x46, 0x54, 0x62, 0x70,
0x82, 0x90, 0xA6, 0xB4, 0xCA, 0xD8, 0xEE, 0xFC, 0x12, 0x00, 0x36, 0x24, 0x5A, 0x48, 0x7E, 0x6C,
0xB0, 0xA2, 0x94, 0x86, 0xF8, 0xEA, 0xDC, 0xCE, 0x20, 0x32, 0x04, 0x16, 0x68, 0x7A, 0x4C, 0x5E,
0xE6, 0xF4, 0xC2, 0xD0, 0xAE, 0xBC, 0x8A, 0x98, 0x76, 0x64, 0x52, 0x40, 0x3E, 0x2C, 0x1A, 0x08,
0xD4, 0xC6, 0xF0, 0xE2, 0x9C, 0x8E, 0xB8, 0xAA, 0x44, 0x56, 0x60, 0x72, 0x0C, 0x1E, 0x28, 0x3A,
0x4A, 0x58, 0x6E, 0x7C, 0x02, 0x10, 0x26, 0x34, 0xDA, 0xC8, 0xFE, 0xEC, 0x92, 0x80, 0xB6, 0xA4,
0x78, 0x6A, 0x5C, 0x4E, 0x30, 0x22, 0x14, 0x06, 0xE8, 0xFA, 0xCC, 0xDE, 0xA0, 0xB2, 0x84, 0x96,
0x2E, 0x3C, 0x0A, 0x18, 0x66, 0x74, 0x42, 0x50, 0xBE, 0xAC, 0x9A, 0x88, 0xF6, 0xE4, 0xD2, 0xC0,
0x1C, 0x0E, 0x38, 0x2A, 0x54, 0x46, 0x70, 0x62, 0x8C, 0x9E, 0xA8, 0xBA, 0xC4, 0xD6, 0xE0, 0xF2,
};
void crc7_mmc_GenerateTable()
{
    uint8_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x80)
                crc = (crc << 1) ^ 0x12;        // 0x12 = 0x09<<(8-7)
            else
                crc = (crc << 1);
        }
        crc7MMC_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("uint8_t crc7MMC_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc7MMC_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}


uint8_t crc7_mmc_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc7MMC_table[tmp];
    }
    return crc >> 1;
}


/*
 * Name:    CRC-7/MMC           x7+x3+1
 * Note:    d for直接计算
 */
uint8_t crc7_mmc_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for ( i = 0; i < 8; i++ )
        {
            if ( crc & 0x80 )
                crc = (crc << 1) ^ 0x12;        // 0x12 = 0x09<<(8-7)
            else
                crc <<= 1;
        }
    }
    return crc >> 1;
}


/******************************************************************************
 * Name:    CRC-8               x8+x2+x+1
 * Poly:    0x07
 * Init:    0x00
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Note:
 *****************************************************************************/

static uint8_t crc8_table[256] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3,
};
/* crc8_GenerateTable */
void crc8_GenerateTable()
{
    uint8_t crc = 0;        // Initial value
    for(uint16_t i =0; i<256; i++){
        crc = i;
        for(uint16_t j=0; j<8; j++ ){
            if( crc & 0x80 ){
                crc = (crc << 1) ^ 0x07;
            }
            else{
                 crc = (crc << 1) ;
            }
        }
        crc8_table[i] = crc;
    }
}
/* CRC8_calc */
uint8_t crc8_calc(uint8_t *data, uint32_t length){
    uint8_t crc = 0;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ *data++;
        crc = crc8_table[tmp];
    }
    return crc;
}

/*
 * Name:    CRC-8               x8+x2+x+1
 * Note:    d for 直接计算
 */
uint8_t crc8_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for ( i = 0; i < 8; i++ )
        {
            if ( crc & 0x80 )
                crc = (crc << 1) ^ 0x07;
            else
                crc <<= 1;
        }
    }
    return crc;
}

/******************************************************************************
 * Name:    CRC-8/ITU           x8+x2+x+1
 * Poly:    0x07
 * Init:    0x00
 * Refin:   False
 * Refout:  False
 * Xorout:  0x55
 * Alias:   CRC-8/ATM
 *****************************************************************************/

uint8_t crc8_itu_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;      // crc ^= *data; data++;
        crc = crc8_table[tmp];    // 与crc8_table表格一致
    }
    return crc^0x55;
}
/******************************************************************************
 * Name:    CRC-8/ITU           x8+x2+x+1
 * Note:    d for直接计算
 *****************************************************************************/

uint8_t crc8_itu_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for ( i = 0; i < 8; i++ )
        {
            if ( crc & 0x80 )
                crc = (crc << 1) ^ 0x07;
            else
                crc <<= 1;
        }
    }
    return crc ^ 0x55;
}

/******************************************************************************
 * Name:    CRC-8/ROHC          x8+x2+x+1
 * Poly:    0x07
 * Init:    0xFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc8ROHC_table[256] = {
0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF,
};
void crc8_rohc_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xE0;        // 0xE0 = reverse 0x07
            else
                crc = (crc >> 1);
        }
        crc8ROHC_table[i] = crc;
    }

/* 用于打印数组 */
//    printf("static uint8_t crc8ROHC_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8ROHC_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

/* CRC8_calc */
uint8_t crc8_rohc_Calc(uint8_t *data, uint32_t length){
    uint8_t crc = 0xFF;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ *data++;
        crc = crc8ROHC_table[tmp];
    }
    return crc;
}
/*
 * Name:    CRC-8/ROHC          x8+x2+x+1
 * Note:    d for直接计算
 */
uint8_t crc8_rohc_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0xFF;         // Initial value
    while(length--)
    {
        crc ^= *data++;            // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xE0; // 0xE0 = reverse 0x07
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-8/MAXIM         x8+x5+x4+1
 * Poly:    0x31
 * Init:    0x00
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Alias:   DOW-CRC,CRC-8/IBUTTON
 * Use:     Maxim(Dallas)'s some devices,e.g. DS18B20
 *****************************************************************************/
static uint8_t crc8MAXIM_table[256] = {
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E, 0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC,
0x23, 0x7D, 0x9F, 0xC1, 0x42, 0x1C, 0xFE, 0xA0, 0xE1, 0xBF, 0x5D, 0x03, 0x80, 0xDE, 0x3C, 0x62,
0xBE, 0xE0, 0x02, 0x5C, 0xDF, 0x81, 0x63, 0x3D, 0x7C, 0x22, 0xC0, 0x9E, 0x1D, 0x43, 0xA1, 0xFF,
0x46, 0x18, 0xFA, 0xA4, 0x27, 0x79, 0x9B, 0xC5, 0x84, 0xDA, 0x38, 0x66, 0xE5, 0xBB, 0x59, 0x07,
0xDB, 0x85, 0x67, 0x39, 0xBA, 0xE4, 0x06, 0x58, 0x19, 0x47, 0xA5, 0xFB, 0x78, 0x26, 0xC4, 0x9A,
0x65, 0x3B, 0xD9, 0x87, 0x04, 0x5A, 0xB8, 0xE6, 0xA7, 0xF9, 0x1B, 0x45, 0xC6, 0x98, 0x7A, 0x24,
0xF8, 0xA6, 0x44, 0x1A, 0x99, 0xC7, 0x25, 0x7B, 0x3A, 0x64, 0x86, 0xD8, 0x5B, 0x05, 0xE7, 0xB9,
0x8C, 0xD2, 0x30, 0x6E, 0xED, 0xB3, 0x51, 0x0F, 0x4E, 0x10, 0xF2, 0xAC, 0x2F, 0x71, 0x93, 0xCD,
0x11, 0x4F, 0xAD, 0xF3, 0x70, 0x2E, 0xCC, 0x92, 0xD3, 0x8D, 0x6F, 0x31, 0xB2, 0xEC, 0x0E, 0x50,
0xAF, 0xF1, 0x13, 0x4D, 0xCE, 0x90, 0x72, 0x2C, 0x6D, 0x33, 0xD1, 0x8F, 0x0C, 0x52, 0xB0, 0xEE,
0x32, 0x6C, 0x8E, 0xD0, 0x53, 0x0D, 0xEF, 0xB1, 0xF0, 0xAE, 0x4C, 0x12, 0x91, 0xCF, 0x2D, 0x73,
0xCA, 0x94, 0x76, 0x28, 0xAB, 0xF5, 0x17, 0x49, 0x08, 0x56, 0xB4, 0xEA, 0x69, 0x37, 0xD5, 0x8B,
0x57, 0x09, 0xEB, 0xB5, 0x36, 0x68, 0x8A, 0xD4, 0x95, 0xCB, 0x29, 0x77, 0xF4, 0xAA, 0x48, 0x16,
0xE9, 0xB7, 0x55, 0x0B, 0x88, 0xD6, 0x34, 0x6A, 0x2B, 0x75, 0x97, 0xC9, 0x4A, 0x14, 0xF6, 0xA8,
0x74, 0x2A, 0xC8, 0x96, 0x15, 0x4B, 0xA9, 0xF7, 0xB6, 0xE8, 0x0A, 0x54, 0xD7, 0x89, 0x6B, 0x35,
};
void crc8_maxim_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x8C;        // 0x8C = reverse 0x31
            else
                crc = (crc >> 1);
        }
        crc8MAXIM_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8MAXIM_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8MAXIM_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint8_t crc8_maxim_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc8MAXIM_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-8/MAXIM         x8+x5+x4+1
 * Note:    d for直接计算
 *****************************************************************************/

uint8_t crc8_maxim_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint8_t crc = 0;         // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for (i = 0; i < 8; i++)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x8C;        // 0x8C = reverse 0x31
            else
                crc >>= 1;
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-8/CDMA2000          x8+x7+x4+x3+x+1
 * Poly:    0x9B
 * Init:    0xFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc8CDMA2000_table[256] = {
0x00, 0x9B, 0xAD, 0x36, 0xC1, 0x5A, 0x6C, 0xF7, 0x19, 0x82, 0xB4, 0x2F, 0xD8, 0x43, 0x75, 0xEE,
0x32, 0xA9, 0x9F, 0x04, 0xF3, 0x68, 0x5E, 0xC5, 0x2B, 0xB0, 0x86, 0x1D, 0xEA, 0x71, 0x47, 0xDC,
0x64, 0xFF, 0xC9, 0x52, 0xA5, 0x3E, 0x08, 0x93, 0x7D, 0xE6, 0xD0, 0x4B, 0xBC, 0x27, 0x11, 0x8A,
0x56, 0xCD, 0xFB, 0x60, 0x97, 0x0C, 0x3A, 0xA1, 0x4F, 0xD4, 0xE2, 0x79, 0x8E, 0x15, 0x23, 0xB8,
0xC8, 0x53, 0x65, 0xFE, 0x09, 0x92, 0xA4, 0x3F, 0xD1, 0x4A, 0x7C, 0xE7, 0x10, 0x8B, 0xBD, 0x26,
0xFA, 0x61, 0x57, 0xCC, 0x3B, 0xA0, 0x96, 0x0D, 0xE3, 0x78, 0x4E, 0xD5, 0x22, 0xB9, 0x8F, 0x14,
0xAC, 0x37, 0x01, 0x9A, 0x6D, 0xF6, 0xC0, 0x5B, 0xB5, 0x2E, 0x18, 0x83, 0x74, 0xEF, 0xD9, 0x42,
0x9E, 0x05, 0x33, 0xA8, 0x5F, 0xC4, 0xF2, 0x69, 0x87, 0x1C, 0x2A, 0xB1, 0x46, 0xDD, 0xEB, 0x70,
0x0B, 0x90, 0xA6, 0x3D, 0xCA, 0x51, 0x67, 0xFC, 0x12, 0x89, 0xBF, 0x24, 0xD3, 0x48, 0x7E, 0xE5,
0x39, 0xA2, 0x94, 0x0F, 0xF8, 0x63, 0x55, 0xCE, 0x20, 0xBB, 0x8D, 0x16, 0xE1, 0x7A, 0x4C, 0xD7,
0x6F, 0xF4, 0xC2, 0x59, 0xAE, 0x35, 0x03, 0x98, 0x76, 0xED, 0xDB, 0x40, 0xB7, 0x2C, 0x1A, 0x81,
0x5D, 0xC6, 0xF0, 0x6B, 0x9C, 0x07, 0x31, 0xAA, 0x44, 0xDF, 0xE9, 0x72, 0x85, 0x1E, 0x28, 0xB3,
0xC3, 0x58, 0x6E, 0xF5, 0x02, 0x99, 0xAF, 0x34, 0xDA, 0x41, 0x77, 0xEC, 0x1B, 0x80, 0xB6, 0x2D,
0xF1, 0x6A, 0x5C, 0xC7, 0x30, 0xAB, 0x9D, 0x06, 0xE8, 0x73, 0x45, 0xDE, 0x29, 0xB2, 0x84, 0x1F,
0xA7, 0x3C, 0x0A, 0x91, 0x66, 0xFD, 0xCB, 0x50, 0xBE, 0x25, 0x13, 0x88, 0x7F, 0xE4, 0xD2, 0x49,
0x95, 0x0E, 0x38, 0xA3, 0x54, 0xCF, 0xF9, 0x62, 0x8C, 0x17, 0x21, 0xBA, 0x4D, 0xD6, 0xE0, 0x7B,
};

/* crc8_GenerateTable */
void crc8_cdma2000_GenerateTable()
{
    uint8_t crc = 0;        // Initial value
    for(uint16_t i =0; i<256; i++){
        crc = i;
        for(uint16_t j=0; j<8; j++ ){
            if( crc & 0x80 ){
                crc = (crc << 1) ^ 0x9B;
            }
            else{
                 crc = (crc << 1) ;
            }
        }
        crc8CDMA2000_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8CDMA2000_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8CDMA2000_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}
/* crc8_cdma2000_Calc */
uint8_t crc8_cdma2000_calc(uint8_t *data, uint32_t length){
    uint8_t crc = 0xFF;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ *data++;
        crc = crc8CDMA2000_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-8/DARC         x8+x5+x4+x3+1
 * Poly:    0x39
 * Init:    0x00
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Alias:
 *****************************************************************************/
static uint8_t crc8DARC_table[256] = {
0x00, 0x72, 0xE4, 0x96, 0xF1, 0x83, 0x15, 0x67, 0xDB, 0xA9, 0x3F, 0x4D, 0x2A, 0x58, 0xCE, 0xBC,
0x8F, 0xFD, 0x6B, 0x19, 0x7E, 0x0C, 0x9A, 0xE8, 0x54, 0x26, 0xB0, 0xC2, 0xA5, 0xD7, 0x41, 0x33,
0x27, 0x55, 0xC3, 0xB1, 0xD6, 0xA4, 0x32, 0x40, 0xFC, 0x8E, 0x18, 0x6A, 0x0D, 0x7F, 0xE9, 0x9B,
0xA8, 0xDA, 0x4C, 0x3E, 0x59, 0x2B, 0xBD, 0xCF, 0x73, 0x01, 0x97, 0xE5, 0x82, 0xF0, 0x66, 0x14,
0x4E, 0x3C, 0xAA, 0xD8, 0xBF, 0xCD, 0x5B, 0x29, 0x95, 0xE7, 0x71, 0x03, 0x64, 0x16, 0x80, 0xF2,
0xC1, 0xB3, 0x25, 0x57, 0x30, 0x42, 0xD4, 0xA6, 0x1A, 0x68, 0xFE, 0x8C, 0xEB, 0x99, 0x0F, 0x7D,
0x69, 0x1B, 0x8D, 0xFF, 0x98, 0xEA, 0x7C, 0x0E, 0xB2, 0xC0, 0x56, 0x24, 0x43, 0x31, 0xA7, 0xD5,
0xE6, 0x94, 0x02, 0x70, 0x17, 0x65, 0xF3, 0x81, 0x3D, 0x4F, 0xD9, 0xAB, 0xCC, 0xBE, 0x28, 0x5A,
0x9C, 0xEE, 0x78, 0x0A, 0x6D, 0x1F, 0x89, 0xFB, 0x47, 0x35, 0xA3, 0xD1, 0xB6, 0xC4, 0x52, 0x20,
0x13, 0x61, 0xF7, 0x85, 0xE2, 0x90, 0x06, 0x74, 0xC8, 0xBA, 0x2C, 0x5E, 0x39, 0x4B, 0xDD, 0xAF,
0xBB, 0xC9, 0x5F, 0x2D, 0x4A, 0x38, 0xAE, 0xDC, 0x60, 0x12, 0x84, 0xF6, 0x91, 0xE3, 0x75, 0x07,
0x34, 0x46, 0xD0, 0xA2, 0xC5, 0xB7, 0x21, 0x53, 0xEF, 0x9D, 0x0B, 0x79, 0x1E, 0x6C, 0xFA, 0x88,
0xD2, 0xA0, 0x36, 0x44, 0x23, 0x51, 0xC7, 0xB5, 0x09, 0x7B, 0xED, 0x9F, 0xF8, 0x8A, 0x1C, 0x6E,
0x5D, 0x2F, 0xB9, 0xCB, 0xAC, 0xDE, 0x48, 0x3A, 0x86, 0xF4, 0x62, 0x10, 0x77, 0x05, 0x93, 0xE1,
0xF5, 0x87, 0x11, 0x63, 0x04, 0x76, 0xE0, 0x92, 0x2E, 0x5C, 0xCA, 0xB8, 0xDF, 0xAD, 0x3B, 0x49,
0x7A, 0x08, 0x9E, 0xEC, 0x8B, 0xF9, 0x6F, 0x1D, 0xA1, 0xD3, 0x45, 0x37, 0x50, 0x22, 0xB4, 0xC6,
};
void crc8_darc_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x9C;        // 0x9C = reverse 0x39
            else
                crc = (crc >> 1);
        }
        crc8DARC_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8DARC_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8DARC_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint8_t crc8_darc_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc8DARC_table[tmp];
    }
    return crc;
}

/******************************************************************************
 * Name:    CRC-8/DVB-S2          x8+x7+x6+x4+x2+x+1
 * Poly:    0xD5
 * Init:    0x00
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc8DVB_S2_table[256] = {
0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54, 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06, 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0, 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2, 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9, 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B, 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D, 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F, 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB, 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9, 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F, 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D, 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26, 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74, 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82, 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0, 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9,
};

/* crc8_GenerateTable */
void crc8_dvb_s2_GenerateTable()
{
    uint8_t crc = 0;        // Initial value
    for(uint16_t i =0; i<256; i++){
        crc = i;
        for(uint16_t j=0; j<8; j++ ){
            if( crc & 0x80 ){
                crc = (crc << 1) ^ 0xD5;
            }
            else{
                 crc = (crc << 1) ;
            }
        }
        crc8DVB_S2_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8DVB_S2_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8DVB_S2_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}
/* crc8_dvb_s2_calc */
uint8_t crc8_dvb_s2_calc(uint8_t *data, uint32_t length){
    uint8_t crc = 0;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ *data++;
        crc = crc8DVB_S2_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-8/EBU         x8+x4++x3+x2+1
 * Poly:    0x1D
 * Init:    0xFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Alias:
 *****************************************************************************/
static uint8_t crc8EBU_table[256] = {
0x00, 0x64, 0xC8, 0xAC, 0xE1, 0x85, 0x29, 0x4D, 0xB3, 0xD7, 0x7B, 0x1F, 0x52, 0x36, 0x9A, 0xFE,
0x17, 0x73, 0xDF, 0xBB, 0xF6, 0x92, 0x3E, 0x5A, 0xA4, 0xC0, 0x6C, 0x08, 0x45, 0x21, 0x8D, 0xE9,
0x2E, 0x4A, 0xE6, 0x82, 0xCF, 0xAB, 0x07, 0x63, 0x9D, 0xF9, 0x55, 0x31, 0x7C, 0x18, 0xB4, 0xD0,
0x39, 0x5D, 0xF1, 0x95, 0xD8, 0xBC, 0x10, 0x74, 0x8A, 0xEE, 0x42, 0x26, 0x6B, 0x0F, 0xA3, 0xC7,
0x5C, 0x38, 0x94, 0xF0, 0xBD, 0xD9, 0x75, 0x11, 0xEF, 0x8B, 0x27, 0x43, 0x0E, 0x6A, 0xC6, 0xA2,
0x4B, 0x2F, 0x83, 0xE7, 0xAA, 0xCE, 0x62, 0x06, 0xF8, 0x9C, 0x30, 0x54, 0x19, 0x7D, 0xD1, 0xB5,
0x72, 0x16, 0xBA, 0xDE, 0x93, 0xF7, 0x5B, 0x3F, 0xC1, 0xA5, 0x09, 0x6D, 0x20, 0x44, 0xE8, 0x8C,
0x65, 0x01, 0xAD, 0xC9, 0x84, 0xE0, 0x4C, 0x28, 0xD6, 0xB2, 0x1E, 0x7A, 0x37, 0x53, 0xFF, 0x9B,
0xB8, 0xDC, 0x70, 0x14, 0x59, 0x3D, 0x91, 0xF5, 0x0B, 0x6F, 0xC3, 0xA7, 0xEA, 0x8E, 0x22, 0x46,
0xAF, 0xCB, 0x67, 0x03, 0x4E, 0x2A, 0x86, 0xE2, 0x1C, 0x78, 0xD4, 0xB0, 0xFD, 0x99, 0x35, 0x51,
0x96, 0xF2, 0x5E, 0x3A, 0x77, 0x13, 0xBF, 0xDB, 0x25, 0x41, 0xED, 0x89, 0xC4, 0xA0, 0x0C, 0x68,
0x81, 0xE5, 0x49, 0x2D, 0x60, 0x04, 0xA8, 0xCC, 0x32, 0x56, 0xFA, 0x9E, 0xD3, 0xB7, 0x1B, 0x7F,
0xE4, 0x80, 0x2C, 0x48, 0x05, 0x61, 0xCD, 0xA9, 0x57, 0x33, 0x9F, 0xFB, 0xB6, 0xD2, 0x7E, 0x1A,
0xF3, 0x97, 0x3B, 0x5F, 0x12, 0x76, 0xDA, 0xBE, 0x40, 0x24, 0x88, 0xEC, 0xA1, 0xC5, 0x69, 0x0D,
0xCA, 0xAE, 0x02, 0x66, 0x2B, 0x4F, 0xE3, 0x87, 0x79, 0x1D, 0xB1, 0xD5, 0x98, 0xFC, 0x50, 0x34,
0xDD, 0xB9, 0x15, 0x71, 0x3C, 0x58, 0xF4, 0x90, 0x6E, 0x0A, 0xA6, 0xC2, 0x8F, 0xEB, 0x47, 0x23,
};
void crc8_ebu_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xB8;        // 0xB8 = reverse 0x1D
            else
                crc = (crc >> 1);
        }
        crc8EBU_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8EBU_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8EBU_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint8_t crc8_ebu_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0xFF;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc8EBU_table[tmp];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-8/I-CODE          x8+x7+x6+x4+x2+x+1
 * Poly:    0x1D
 * Init:    0xFD
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint8_t crc8I_CODE_table[256] = {
0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54, 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06, 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0, 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2, 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9, 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B, 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D, 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F, 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB, 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9, 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F, 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D, 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26, 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74, 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82, 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0, 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9,
};

/* crc8_GenerateTable */
void crc8_i_code_GenerateTable()
{
    uint8_t crc = 0;        // Initial value
    for(uint16_t i =0; i<256; i++){
        crc = i;
        for(uint16_t j=0; j<8; j++ ){
            if( crc & 0x80 ){
                crc = (crc << 1) ^ 0x1D;
            }
            else{
                 crc = (crc << 1) ;
            }
        }
        crc8I_CODE_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8I_CODE_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ", crc8I_CODE_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}
/* crc8_i_code_calc */
uint8_t crc8_i_code_calc(uint8_t *data, uint32_t length){
    uint8_t crc = 0xFD;
    uint8_t tmp = 0;
    while(length--){
        tmp = crc ^ *data++;
        crc = crc8I_CODE_table[tmp];
    }
    return crc;
}

/******************************************************************************
 * Name:    CRC-8/WCDMA          x8+x7+x4+x3+x+1
 * Poly:    0x9B
 * Init:    0x00
 * Refin:   True
 * Refout:  True
 * Xorout:  0x00
 * Alias:
 *****************************************************************************/
static uint8_t crc8WCDMA_table[256] = {
0x00, 0x64, 0xC8, 0xAC, 0xE1, 0x85, 0x29, 0x4D, 0xB3, 0xD7, 0x7B, 0x1F, 0x52, 0x36, 0x9A, 0xFE,
0x17, 0x73, 0xDF, 0xBB, 0xF6, 0x92, 0x3E, 0x5A, 0xA4, 0xC0, 0x6C, 0x08, 0x45, 0x21, 0x8D, 0xE9,
0x2E, 0x4A, 0xE6, 0x82, 0xCF, 0xAB, 0x07, 0x63, 0x9D, 0xF9, 0x55, 0x31, 0x7C, 0x18, 0xB4, 0xD0,
0x39, 0x5D, 0xF1, 0x95, 0xD8, 0xBC, 0x10, 0x74, 0x8A, 0xEE, 0x42, 0x26, 0x6B, 0x0F, 0xA3, 0xC7,
0x5C, 0x38, 0x94, 0xF0, 0xBD, 0xD9, 0x75, 0x11, 0xEF, 0x8B, 0x27, 0x43, 0x0E, 0x6A, 0xC6, 0xA2,
0x4B, 0x2F, 0x83, 0xE7, 0xAA, 0xCE, 0x62, 0x06, 0xF8, 0x9C, 0x30, 0x54, 0x19, 0x7D, 0xD1, 0xB5,
0x72, 0x16, 0xBA, 0xDE, 0x93, 0xF7, 0x5B, 0x3F, 0xC1, 0xA5, 0x09, 0x6D, 0x20, 0x44, 0xE8, 0x8C,
0x65, 0x01, 0xAD, 0xC9, 0x84, 0xE0, 0x4C, 0x28, 0xD6, 0xB2, 0x1E, 0x7A, 0x37, 0x53, 0xFF, 0x9B,
0xB8, 0xDC, 0x70, 0x14, 0x59, 0x3D, 0x91, 0xF5, 0x0B, 0x6F, 0xC3, 0xA7, 0xEA, 0x8E, 0x22, 0x46,
0xAF, 0xCB, 0x67, 0x03, 0x4E, 0x2A, 0x86, 0xE2, 0x1C, 0x78, 0xD4, 0xB0, 0xFD, 0x99, 0x35, 0x51,
0x96, 0xF2, 0x5E, 0x3A, 0x77, 0x13, 0xBF, 0xDB, 0x25, 0x41, 0xED, 0x89, 0xC4, 0xA0, 0x0C, 0x68,
0x81, 0xE5, 0x49, 0x2D, 0x60, 0x04, 0xA8, 0xCC, 0x32, 0x56, 0xFA, 0x9E, 0xD3, 0xB7, 0x1B, 0x7F,
0xE4, 0x80, 0x2C, 0x48, 0x05, 0x61, 0xCD, 0xA9, 0x57, 0x33, 0x9F, 0xFB, 0xB6, 0xD2, 0x7E, 0x1A,
0xF3, 0x97, 0x3B, 0x5F, 0x12, 0x76, 0xDA, 0xBE, 0x40, 0x24, 0x88, 0xEC, 0xA1, 0xC5, 0x69, 0x0D,
0xCA, 0xAE, 0x02, 0x66, 0x2B, 0x4F, 0xE3, 0x87, 0x79, 0x1D, 0xB1, 0xD5, 0x98, 0xFC, 0x50, 0x34,
0xDD, 0xB9, 0x15, 0x71, 0x3C, 0x58, 0xF4, 0x90, 0x6E, 0x0A, 0xA6, 0xC2, 0x8F, 0xEB, 0x47, 0x23,
};
void crc8_wcdma_GenerateTable()
{
    uint8_t crc = 0;         // Initial value
    for(uint16_t i=0; i<256; i++){
        crc = i;
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xD9;        // 0xD9 = reverse 0x9B
            else
                crc = (crc >> 1);
        }
        crc8WCDMA_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint8_t crc8WCDMA_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%02X, ",crc8WCDMA_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint8_t crc8_wcdma_calc(uint8_t *data, uint32_t length)
{
    uint8_t tmp = 0;
    uint8_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = crc ^ *data++;                 // crc ^= *data; data++;
        crc = crc8WCDMA_table[tmp];
    }
    return crc;
}


/******************************************************************************
 * Name:    CRC-16/IBM          x16+x15+x2+1
 * Poly:    0x8005
 * Init:    0x0000
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Alias:   CRC-16,CRC-16/ARC,CRC-16/LHA
 *****************************************************************************/
static uint16_t crc16IBM_table[256] = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040,
};
void crc16_ibm_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xA001;        // 0xA001 = reverse 0x8005
            else
                crc = (crc >> 1);
        }
        crc16IBM_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16IBM_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16IBM_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_ibm_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16IBM_table[tmp&0xFF];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-16/IBM          x16+x15+x2+1
 * Note:    d for直接计算
 *****************************************************************************/

uint16_t crc16_ibm_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA001;        // 0xA001 = reverse 0x8005
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}

/******************************************************************************
 * Name:    CRC-16/MAXIM        x16+x15+x2+1
 * Poly:    0x8005
 * Init:    0x0000
 * Refin:   True
 * Refout:  True
 * Xorout:  0xFFFF
 * Note:
 *****************************************************************************/


uint16_t crc16_maxim_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16IBM_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFF
}

/******************************************************************************
 * Name:    CRC-16/MAXIM          x16+x15+x2+1
 * Note:    d for直接计算
 *****************************************************************************/

uint16_t crc16_maxim_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA001;        // 0xA001 = reverse 0x8005
            else
                crc = (crc >> 1);
        }
    }
    return ~crc;    // crc^0xffff
}
/******************************************************************************
 * Name:    CRC-16/USB          x16+x15+x2+1
 * Poly:    0x8005
 * Init:    0xFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0xFFFF
 * Note:
 *****************************************************************************/

uint16_t crc16_usb_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16IBM_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFF
}

/******************************************************************************
 * Name:    CRC-16/USB          x16+x15+x2+1
 * Note:    d for直接计算
 *****************************************************************************/

uint16_t crc16_usb_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0xffff;        // Initial value
    while(length--)
    {
        crc ^= *data++;            // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA001;        // 0xA001 = reverse 0x8005
            else
                crc = (crc >> 1);
        }
    }
    return ~crc;    // crc^0xffff
}
/******************************************************************************
 * Name:    CRC-16/MODBUS       x16+x15+x2+1
 * Poly:    0x8005
 * Init:    0xFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
uint16_t crc16_modbus_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16IBM_table[tmp&0xFF];
    }
    return crc; // crc
}


/******************************************************************************
 * Name:    CRC-16/MODBUS       x16+x15+x2+1
 * Note:    d for直接计算
 *****************************************************************************/

uint16_t crc16_modbus_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0xffff;        // Initial value
    while(length--)
    {
        crc ^= *data++;            // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA001;        // 0xA001 = reverse 0x8005
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}

/******************************************************************************
 * Name:    CRC-16/CCITT        x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0x0000
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Alias:   CRC-CCITT,CRC-16/CCITT-TRUE,CRC-16/KERMIT
 *****************************************************************************/
static uint16_t crc16CCITT_table[256] = {
0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738, 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, 0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78,
};

void crc16_ccitt_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x8408;        // 0x8408 = reverse 0x1021
            else
                crc = (crc >> 1);
        }
        crc16CCITT_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16CCITT_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16CCITT_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_ccitt_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16CCITT_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-16/CCITT        x16+x12+x5+1
 * Note:    d for直接计算
 *****************************************************************************/
uint16_t crc16_ccitt_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x8408;        // 0x8408 = reverse 0x1021
            else
                crc = (crc >> 1);
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-16/CCITT-FALSE   x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0xFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
static uint16_t crc16CCITT_false_table[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
};


void crc16_ccitt_false_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x1021;
            else
                crc = (crc << 1);
        }
        crc16CCITT_false_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16CCITT_false_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16CCITT_false_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_ccitt_false_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16CCITT_false_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-16/CCITT-FALSE   x16+x12+x5+1
 * Note:    d for直接计算
 *****************************************************************************/
uint16_t crc16_ccitt_false_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0xffff;        //Initial value
    while(length--)
    {
        crc ^= (uint16_t)(*data++) << 8; // crc ^= (uint6_t)(*data)<<8; data++;
        for (i = 0; i < 8; ++i)
        {
            if ( crc & 0x8000 )
                crc = (crc << 1) ^ 0x1021;
            else
                crc <<= 1;
        }
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-16/X25          x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0xFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0XFFFF
 * Note:
 *****************************************************************************/

uint16_t crc16_x25_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16CCITT_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFF
}

/******************************************************************************
 * Name:    CRC-16/X25          x16+x12+x5+1
 * Note:    d for直接计算
 *****************************************************************************/

uint16_t crc16_x25_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0xffff;        // Initial value
    while(length--)
    {
        crc ^= *data++;            // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0x8408;        // 0x8408 = reverse 0x1021
            else
                crc = (crc >> 1);
        }
    }
    return ~crc;                // crc^Xorout
}

/******************************************************************************
 * Name:    CRC-16/XMODEM       x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Alias:   CRC-16/ZMODEM,CRC-16/ACORN
 *****************************************************************************/

uint16_t crc16_xmodem_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16CCITT_false_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-16/XMODEM       x16+x12+x5+1
 * Note:    d for直接计算
 *****************************************************************************/
uint16_t crc16_xmodem_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0;            // Initial value
    while(length--)
    {
        crc ^= (uint16_t)(*data++) << 8; // crc ^= (uint16_t)(*data)<<8; data++;
        for (i = 0; i < 8; ++i)
        {
            if ( crc & 0x8000 )
                crc = (crc << 1) ^ 0x1021;
            else
                crc <<= 1;
        }
    }
    return crc;
}


/******************************************************************************
 * Name:    CRC-16/DNP          x16+x13+x12+x11+x10+x8+x6+x5+x2+1
 * Poly:    0x3D65
 * Init:    0x0000
 * Refin:   True
 * Refout:  True
 * Xorout:  0xFFFF
 * Use:     M-Bus,ect.
 *****************************************************************************/

static uint16_t crc16DNP_table[256] = {
0x0000, 0x365E, 0x6CBC, 0x5AE2, 0xD978, 0xEF26, 0xB5C4, 0x839A, 0xFF89, 0xC9D7, 0x9335, 0xA56B, 0x26F1, 0x10AF, 0x4A4D, 0x7C13,
0xB26B, 0x8435, 0xDED7, 0xE889, 0x6B13, 0x5D4D, 0x07AF, 0x31F1, 0x4DE2, 0x7BBC, 0x215E, 0x1700, 0x949A, 0xA2C4, 0xF826, 0xCE78,
0x29AF, 0x1FF1, 0x4513, 0x734D, 0xF0D7, 0xC689, 0x9C6B, 0xAA35, 0xD626, 0xE078, 0xBA9A, 0x8CC4, 0x0F5E, 0x3900, 0x63E2, 0x55BC,
0x9BC4, 0xAD9A, 0xF778, 0xC126, 0x42BC, 0x74E2, 0x2E00, 0x185E, 0x644D, 0x5213, 0x08F1, 0x3EAF, 0xBD35, 0x8B6B, 0xD189, 0xE7D7,
0x535E, 0x6500, 0x3FE2, 0x09BC, 0x8A26, 0xBC78, 0xE69A, 0xD0C4, 0xACD7, 0x9A89, 0xC06B, 0xF635, 0x75AF, 0x43F1, 0x1913, 0x2F4D,
0xE135, 0xD76B, 0x8D89, 0xBBD7, 0x384D, 0x0E13, 0x54F1, 0x62AF, 0x1EBC, 0x28E2, 0x7200, 0x445E, 0xC7C4, 0xF19A, 0xAB78, 0x9D26,
0x7AF1, 0x4CAF, 0x164D, 0x2013, 0xA389, 0x95D7, 0xCF35, 0xF96B, 0x8578, 0xB326, 0xE9C4, 0xDF9A, 0x5C00, 0x6A5E, 0x30BC, 0x06E2,
0xC89A, 0xFEC4, 0xA426, 0x9278, 0x11E2, 0x27BC, 0x7D5E, 0x4B00, 0x3713, 0x014D, 0x5BAF, 0x6DF1, 0xEE6B, 0xD835, 0x82D7, 0xB489,
0xA6BC, 0x90E2, 0xCA00, 0xFC5E, 0x7FC4, 0x499A, 0x1378, 0x2526, 0x5935, 0x6F6B, 0x3589, 0x03D7, 0x804D, 0xB613, 0xECF1, 0xDAAF,
0x14D7, 0x2289, 0x786B, 0x4E35, 0xCDAF, 0xFBF1, 0xA113, 0x974D, 0xEB5E, 0xDD00, 0x87E2, 0xB1BC, 0x3226, 0x0478, 0x5E9A, 0x68C4,
0x8F13, 0xB94D, 0xE3AF, 0xD5F1, 0x566B, 0x6035, 0x3AD7, 0x0C89, 0x709A, 0x46C4, 0x1C26, 0x2A78, 0xA9E2, 0x9FBC, 0xC55E, 0xF300,
0x3D78, 0x0B26, 0x51C4, 0x679A, 0xE400, 0xD25E, 0x88BC, 0xBEE2, 0xC2F1, 0xF4AF, 0xAE4D, 0x9813, 0x1B89, 0x2DD7, 0x7735, 0x416B,
0xF5E2, 0xC3BC, 0x995E, 0xAF00, 0x2C9A, 0x1AC4, 0x4026, 0x7678, 0x0A6B, 0x3C35, 0x66D7, 0x5089, 0xD313, 0xE54D, 0xBFAF, 0x89F1,
0x4789, 0x71D7, 0x2B35, 0x1D6B, 0x9EF1, 0xA8AF, 0xF24D, 0xC413, 0xB800, 0x8E5E, 0xD4BC, 0xE2E2, 0x6178, 0x5726, 0x0DC4, 0x3B9A,
0xDC4D, 0xEA13, 0xB0F1, 0x86AF, 0x0535, 0x336B, 0x6989, 0x5FD7, 0x23C4, 0x159A, 0x4F78, 0x7926, 0xFABC, 0xCCE2, 0x9600, 0xA05E,
0x6E26, 0x5878, 0x029A, 0x34C4, 0xB75E, 0x8100, 0xDBE2, 0xEDBC, 0x91AF, 0xA7F1, 0xFD13, 0xCB4D, 0x48D7, 0x7E89, 0x246B, 0x1235,
};

void crc16_DNP_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xA6BC;        // 0xA6BC = reverse 0x3D65
            else
                crc = (crc >> 1);
        }
        crc16DNP_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16DNP_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16DNP_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_dnp_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16DNP_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFF
}

/******************************************************************************
 * Name:    CRC-16/DNP          x16+x13+x12+x11+x10+x8+x6+x5+x2+1
 * Note:    d for直接计算
 *****************************************************************************/
uint16_t crc16_dnp_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint16_t crc = 0;            // Initial value
    while(length--)
    {
        crc ^= *data++;            // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA6BC;        // 0xA6BC = reverse 0x3D65
            else
                crc = (crc >> 1);
        }
    }
    return ~crc;   // crc^Xorout
}



/******************************************************************************
 * Name:    CRC-16/AUG-CCITT       x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0x1D0F
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Alias:
 *****************************************************************************/
uint16_t crc16_aug_ccitt_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0x1D0F;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16CCITT_false_table[tmp&0xFF];
    }
    return crc; // crc
}


/******************************************************************************
 * Name:    CRC-16/BUYPASS          x16+x15+x2+1
 * Poly:    0x8005
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Alias:
 *****************************************************************************/
static uint16_t crc16BUTPASS_table[256] = {
0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011, 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072, 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2, 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1, 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192, 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1, 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151, 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132, 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312, 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371, 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1, 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2, 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291, 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2, 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252, 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231, 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202,
};
void crc16_buypass_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x8005;
            else
                crc = (crc << 1);
        }
        crc16BUTPASS_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16BUTPASS_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16BUTPASS_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_buypass_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16BUTPASS_table[tmp&0xFF];
    }
    return crc;
}

/******************************************************************************
 * Name:    CRC-16/CDMA2000          x16+x15+x14+x11+x6+x5+x2+x+1
 * Poly:    0xC867
 * Init:    0xFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00
 * Note:
 *****************************************************************************/
static uint16_t crc16CDMA2000_table[256] = {
0x0000, 0xC867, 0x58A9, 0x90CE, 0xB152, 0x7935, 0xE9FB, 0x219C, 0xAAC3, 0x62A4, 0xF26A, 0x3A0D, 0x1B91, 0xD3F6, 0x4338, 0x8B5F,
0x9DE1, 0x5586, 0xC548, 0x0D2F, 0x2CB3, 0xE4D4, 0x741A, 0xBC7D, 0x3722, 0xFF45, 0x6F8B, 0xA7EC, 0x8670, 0x4E17, 0xDED9, 0x16BE,
0xF3A5, 0x3BC2, 0xAB0C, 0x636B, 0x42F7, 0x8A90, 0x1A5E, 0xD239, 0x5966, 0x9101, 0x01CF, 0xC9A8, 0xE834, 0x2053, 0xB09D, 0x78FA,
0x6E44, 0xA623, 0x36ED, 0xFE8A, 0xDF16, 0x1771, 0x87BF, 0x4FD8, 0xC487, 0x0CE0, 0x9C2E, 0x5449, 0x75D5, 0xBDB2, 0x2D7C, 0xE51B,
0x2F2D, 0xE74A, 0x7784, 0xBFE3, 0x9E7F, 0x5618, 0xC6D6, 0x0EB1, 0x85EE, 0x4D89, 0xDD47, 0x1520, 0x34BC, 0xFCDB, 0x6C15, 0xA472,
0xB2CC, 0x7AAB, 0xEA65, 0x2202, 0x039E, 0xCBF9, 0x5B37, 0x9350, 0x180F, 0xD068, 0x40A6, 0x88C1, 0xA95D, 0x613A, 0xF1F4, 0x3993,
0xDC88, 0x14EF, 0x8421, 0x4C46, 0x6DDA, 0xA5BD, 0x3573, 0xFD14, 0x764B, 0xBE2C, 0x2EE2, 0xE685, 0xC719, 0x0F7E, 0x9FB0, 0x57D7,
0x4169, 0x890E, 0x19C0, 0xD1A7, 0xF03B, 0x385C, 0xA892, 0x60F5, 0xEBAA, 0x23CD, 0xB303, 0x7B64, 0x5AF8, 0x929F, 0x0251, 0xCA36,
0x5E5A, 0x963D, 0x06F3, 0xCE94, 0xEF08, 0x276F, 0xB7A1, 0x7FC6, 0xF499, 0x3CFE, 0xAC30, 0x6457, 0x45CB, 0x8DAC, 0x1D62, 0xD505,
0xC3BB, 0x0BDC, 0x9B12, 0x5375, 0x72E9, 0xBA8E, 0x2A40, 0xE227, 0x6978, 0xA11F, 0x31D1, 0xF9B6, 0xD82A, 0x104D, 0x8083, 0x48E4,
0xADFF, 0x6598, 0xF556, 0x3D31, 0x1CAD, 0xD4CA, 0x4404, 0x8C63, 0x073C, 0xCF5B, 0x5F95, 0x97F2, 0xB66E, 0x7E09, 0xEEC7, 0x26A0,
0x301E, 0xF879, 0x68B7, 0xA0D0, 0x814C, 0x492B, 0xD9E5, 0x1182, 0x9ADD, 0x52BA, 0xC274, 0x0A13, 0x2B8F, 0xE3E8, 0x7326, 0xBB41,
0x7177, 0xB910, 0x29DE, 0xE1B9, 0xC025, 0x0842, 0x988C, 0x50EB, 0xDBB4, 0x13D3, 0x831D, 0x4B7A, 0x6AE6, 0xA281, 0x324F, 0xFA28,
0xEC96, 0x24F1, 0xB43F, 0x7C58, 0x5DC4, 0x95A3, 0x056D, 0xCD0A, 0x4655, 0x8E32, 0x1EFC, 0xD69B, 0xF707, 0x3F60, 0xAFAE, 0x67C9,
0x82D2, 0x4AB5, 0xDA7B, 0x121C, 0x3380, 0xFBE7, 0x6B29, 0xA34E, 0x2811, 0xE076, 0x70B8, 0xB8DF, 0x9943, 0x5124, 0xC1EA, 0x098D,
0x1F33, 0xD754, 0x479A, 0x8FFD, 0xAE61, 0x6606, 0xF6C8, 0x3EAF, 0xB5F0, 0x7D97, 0xED59, 0x253E, 0x04A2, 0xCCC5, 0x5C0B, 0x946C,
};

/* crc16_ccitt_false_GenerateTable */
void crc16_cdma2000_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0xC867;
            else
                crc = (crc << 1);
        }
        crc16CDMA2000_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16CDMA2000_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16CDMA2000_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_cdma2000_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16CDMA2000_table[tmp&0xFF];
    }
    return crc; // crc
}



/******************************************************************************
 * Name:    CRC-16/DDS-110          x16+x15+x2+1
 * Poly:    0x8005
 * Init:    0x800D
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Alias:
 *****************************************************************************/

uint16_t crc16_dds_110_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0x800D;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16BUTPASS_table[tmp&0xFF];
    }
    return crc;
}
/******************************************************************************
 * Name:    CRC-16/DECT-R          x10+x8+x7+x3+x+1
 * Poly:    0x0589
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0001
 * Alias:
 *****************************************************************************/
static uint16_t crc16DECT_R_table[256] = {
0x0000, 0x0589, 0x0B12, 0x0E9B, 0x1624, 0x13AD, 0x1D36, 0x18BF, 0x2C48, 0x29C1, 0x275A, 0x22D3, 0x3A6C, 0x3FE5, 0x317E, 0x34F7,
0x5890, 0x5D19, 0x5382, 0x560B, 0x4EB4, 0x4B3D, 0x45A6, 0x402F, 0x74D8, 0x7151, 0x7FCA, 0x7A43, 0x62FC, 0x6775, 0x69EE, 0x6C67,
0xB120, 0xB4A9, 0xBA32, 0xBFBB, 0xA704, 0xA28D, 0xAC16, 0xA99F, 0x9D68, 0x98E1, 0x967A, 0x93F3, 0x8B4C, 0x8EC5, 0x805E, 0x85D7,
0xE9B0, 0xEC39, 0xE2A2, 0xE72B, 0xFF94, 0xFA1D, 0xF486, 0xF10F, 0xC5F8, 0xC071, 0xCEEA, 0xCB63, 0xD3DC, 0xD655, 0xD8CE, 0xDD47,
0x67C9, 0x6240, 0x6CDB, 0x6952, 0x71ED, 0x7464, 0x7AFF, 0x7F76, 0x4B81, 0x4E08, 0x4093, 0x451A, 0x5DA5, 0x582C, 0x56B7, 0x533E,
0x3F59, 0x3AD0, 0x344B, 0x31C2, 0x297D, 0x2CF4, 0x226F, 0x27E6, 0x1311, 0x1698, 0x1803, 0x1D8A, 0x0535, 0x00BC, 0x0E27, 0x0BAE,
0xD6E9, 0xD360, 0xDDFB, 0xD872, 0xC0CD, 0xC544, 0xCBDF, 0xCE56, 0xFAA1, 0xFF28, 0xF1B3, 0xF43A, 0xEC85, 0xE90C, 0xE797, 0xE21E,
0x8E79, 0x8BF0, 0x856B, 0x80E2, 0x985D, 0x9DD4, 0x934F, 0x96C6, 0xA231, 0xA7B8, 0xA923, 0xACAA, 0xB415, 0xB19C, 0xBF07, 0xBA8E,
0xCF92, 0xCA1B, 0xC480, 0xC109, 0xD9B6, 0xDC3F, 0xD2A4, 0xD72D, 0xE3DA, 0xE653, 0xE8C8, 0xED41, 0xF5FE, 0xF077, 0xFEEC, 0xFB65,
0x9702, 0x928B, 0x9C10, 0x9999, 0x8126, 0x84AF, 0x8A34, 0x8FBD, 0xBB4A, 0xBEC3, 0xB058, 0xB5D1, 0xAD6E, 0xA8E7, 0xA67C, 0xA3F5,
0x7EB2, 0x7B3B, 0x75A0, 0x7029, 0x6896, 0x6D1F, 0x6384, 0x660D, 0x52FA, 0x5773, 0x59E8, 0x5C61, 0x44DE, 0x4157, 0x4FCC, 0x4A45,
0x2622, 0x23AB, 0x2D30, 0x28B9, 0x3006, 0x358F, 0x3B14, 0x3E9D, 0x0A6A, 0x0FE3, 0x0178, 0x04F1, 0x1C4E, 0x19C7, 0x175C, 0x12D5,
0xA85B, 0xADD2, 0xA349, 0xA6C0, 0xBE7F, 0xBBF6, 0xB56D, 0xB0E4, 0x8413, 0x819A, 0x8F01, 0x8A88, 0x9237, 0x97BE, 0x9925, 0x9CAC,
0xF0CB, 0xF542, 0xFBD9, 0xFE50, 0xE6EF, 0xE366, 0xEDFD, 0xE874, 0xDC83, 0xD90A, 0xD791, 0xD218, 0xCAA7, 0xCF2E, 0xC1B5, 0xC43C,
0x197B, 0x1CF2, 0x1269, 0x17E0, 0x0F5F, 0x0AD6, 0x044D, 0x01C4, 0x3533, 0x30BA, 0x3E21, 0x3BA8, 0x2317, 0x269E, 0x2805, 0x2D8C,
0x41EB, 0x4462, 0x4AF9, 0x4F70, 0x57CF, 0x5246, 0x5CDD, 0x5954, 0x6DA3, 0x682A, 0x66B1, 0x6338, 0x7B87, 0x7E0E, 0x7095, 0x751C,
};

/* crc16_ccitt_false_GenerateTable */
void crc16_dect_r_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x0589;
            else
                crc = (crc << 1);
        }
        crc16DECT_R_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16DECT_R_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16DECT_R_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_dect_r_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16DECT_R_table[tmp&0xFF];
    }
    return crc ^ 0x0001; // crc
}

/******************************************************************************
 * Name:    CRC-16/DECT-X          x10+x8+x7+x3+x+1
 * Poly:    0x0589
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Alias:
 *****************************************************************************/
uint16_t crc16_dect_x_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16DECT_R_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-16/EN-13757          x16+x13+x12+x11+x10+x8+x6+x5+x2+1
 * Poly:    0x3D65
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0xFFFF
 * Alias:
 *****************************************************************************/
static uint16_t crc16EN_13757_table[256] = {
0x0000, 0x3D65, 0x7ACA, 0x47AF, 0xF594, 0xC8F1, 0x8F5E, 0xB23B, 0xD64D, 0xEB28, 0xAC87, 0x91E2, 0x23D9, 0x1EBC, 0x5913, 0x6476,
0x91FF, 0xAC9A, 0xEB35, 0xD650, 0x646B, 0x590E, 0x1EA1, 0x23C4, 0x47B2, 0x7AD7, 0x3D78, 0x001D, 0xB226, 0x8F43, 0xC8EC, 0xF589,
0x1E9B, 0x23FE, 0x6451, 0x5934, 0xEB0F, 0xD66A, 0x91C5, 0xACA0, 0xC8D6, 0xF5B3, 0xB21C, 0x8F79, 0x3D42, 0x0027, 0x4788, 0x7AED,
0x8F64, 0xB201, 0xF5AE, 0xC8CB, 0x7AF0, 0x4795, 0x003A, 0x3D5F, 0x5929, 0x644C, 0x23E3, 0x1E86, 0xACBD, 0x91D8, 0xD677, 0xEB12,
0x3D36, 0x0053, 0x47FC, 0x7A99, 0xC8A2, 0xF5C7, 0xB268, 0x8F0D, 0xEB7B, 0xD61E, 0x91B1, 0xACD4, 0x1EEF, 0x238A, 0x6425, 0x5940,
0xACC9, 0x91AC, 0xD603, 0xEB66, 0x595D, 0x6438, 0x2397, 0x1EF2, 0x7A84, 0x47E1, 0x004E, 0x3D2B, 0x8F10, 0xB275, 0xF5DA, 0xC8BF,
0x23AD, 0x1EC8, 0x5967, 0x6402, 0xD639, 0xEB5C, 0xACF3, 0x9196, 0xF5E0, 0xC885, 0x8F2A, 0xB24F, 0x0074, 0x3D11, 0x7ABE, 0x47DB,
0xB252, 0x8F37, 0xC898, 0xF5FD, 0x47C6, 0x7AA3, 0x3D0C, 0x0069, 0x641F, 0x597A, 0x1ED5, 0x23B0, 0x918B, 0xACEE, 0xEB41, 0xD624,
0x7A6C, 0x4709, 0x00A6, 0x3DC3, 0x8FF8, 0xB29D, 0xF532, 0xC857, 0xAC21, 0x9144, 0xD6EB, 0xEB8E, 0x59B5, 0x64D0, 0x237F, 0x1E1A,
0xEB93, 0xD6F6, 0x9159, 0xAC3C, 0x1E07, 0x2362, 0x64CD, 0x59A8, 0x3DDE, 0x00BB, 0x4714, 0x7A71, 0xC84A, 0xF52F, 0xB280, 0x8FE5,
0x64F7, 0x5992, 0x1E3D, 0x2358, 0x9163, 0xAC06, 0xEBA9, 0xD6CC, 0xB2BA, 0x8FDF, 0xC870, 0xF515, 0x472E, 0x7A4B, 0x3DE4, 0x0081,
0xF508, 0xC86D, 0x8FC2, 0xB2A7, 0x009C, 0x3DF9, 0x7A56, 0x4733, 0x2345, 0x1E20, 0x598F, 0x64EA, 0xD6D1, 0xEBB4, 0xAC1B, 0x917E,
0x475A, 0x7A3F, 0x3D90, 0x00F5, 0xB2CE, 0x8FAB, 0xC804, 0xF561, 0x9117, 0xAC72, 0xEBDD, 0xD6B8, 0x6483, 0x59E6, 0x1E49, 0x232C,
0xD6A5, 0xEBC0, 0xAC6F, 0x910A, 0x2331, 0x1E54, 0x59FB, 0x649E, 0x00E8, 0x3D8D, 0x7A22, 0x4747, 0xF57C, 0xC819, 0x8FB6, 0xB2D3,
0x59C1, 0x64A4, 0x230B, 0x1E6E, 0xAC55, 0x9130, 0xD69F, 0xEBFA, 0x8F8C, 0xB2E9, 0xF546, 0xC823, 0x7A18, 0x477D, 0x00D2, 0x3DB7,
0xC83E, 0xF55B, 0xB2F4, 0x8F91, 0x3DAA, 0x00CF, 0x4760, 0x7A05, 0x1E73, 0x2316, 0x64B9, 0x59DC, 0xEBE7, 0xD682, 0x912D, 0xAC48,
};

/* crc16_ccitt_false_GenerateTable */
void crc16_en_13757_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x3D65;
            else
                crc = (crc << 1);
        }
        crc16EN_13757_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16EN_13757_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16EN_13757_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_en_13757_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16EN_13757_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFF
}


/******************************************************************************
 * Name:    CRC-16/GENIBUS      x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0xFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0xFFFF
 * Note:
 *****************************************************************************/

uint16_t crc16_genibus_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16CCITT_false_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFF
}

/******************************************************************************
 * Name:    CRC-16/MCRF4XXX          x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0xFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
uint16_t crc16_mcrf4xx_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0xFFFF;                // Initial value
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16CCITT_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-16/RIELLO          x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0xB2AA
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
uint16_t crc16_riello_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0x554D;                // Initial value    // 0x554D = reverse 0xB2AA
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16CCITT_table[tmp&0xFF];
    }
    return crc; // crc
}
/******************************************************************************
 * Name:    CRC-16/T10-DFIF     x16+x15+x14+x12+x11+x9+x8+x7+x5+x4+x2+x+1
 * Poly:    0x8BB7
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
static uint16_t crc16T10_DIF_table[256] = {
0x0000, 0x8BB7, 0x9CD9, 0x176E, 0xB205, 0x39B2, 0x2EDC, 0xA56B, 0xEFBD, 0x640A, 0x7364, 0xF8D3, 0x5DB8, 0xD60F, 0xC161, 0x4AD6,
0x54CD, 0xDF7A, 0xC814, 0x43A3, 0xE6C8, 0x6D7F, 0x7A11, 0xF1A6, 0xBB70, 0x30C7, 0x27A9, 0xAC1E, 0x0975, 0x82C2, 0x95AC, 0x1E1B,
0xA99A, 0x222D, 0x3543, 0xBEF4, 0x1B9F, 0x9028, 0x8746, 0x0CF1, 0x4627, 0xCD90, 0xDAFE, 0x5149, 0xF422, 0x7F95, 0x68FB, 0xE34C,
0xFD57, 0x76E0, 0x618E, 0xEA39, 0x4F52, 0xC4E5, 0xD38B, 0x583C, 0x12EA, 0x995D, 0x8E33, 0x0584, 0xA0EF, 0x2B58, 0x3C36, 0xB781,
0xD883, 0x5334, 0x445A, 0xCFED, 0x6A86, 0xE131, 0xF65F, 0x7DE8, 0x373E, 0xBC89, 0xABE7, 0x2050, 0x853B, 0x0E8C, 0x19E2, 0x9255,
0x8C4E, 0x07F9, 0x1097, 0x9B20, 0x3E4B, 0xB5FC, 0xA292, 0x2925, 0x63F3, 0xE844, 0xFF2A, 0x749D, 0xD1F6, 0x5A41, 0x4D2F, 0xC698,
0x7119, 0xFAAE, 0xEDC0, 0x6677, 0xC31C, 0x48AB, 0x5FC5, 0xD472, 0x9EA4, 0x1513, 0x027D, 0x89CA, 0x2CA1, 0xA716, 0xB078, 0x3BCF,
0x25D4, 0xAE63, 0xB90D, 0x32BA, 0x97D1, 0x1C66, 0x0B08, 0x80BF, 0xCA69, 0x41DE, 0x56B0, 0xDD07, 0x786C, 0xF3DB, 0xE4B5, 0x6F02,
0x3AB1, 0xB106, 0xA668, 0x2DDF, 0x88B4, 0x0303, 0x146D, 0x9FDA, 0xD50C, 0x5EBB, 0x49D5, 0xC262, 0x6709, 0xECBE, 0xFBD0, 0x7067,
0x6E7C, 0xE5CB, 0xF2A5, 0x7912, 0xDC79, 0x57CE, 0x40A0, 0xCB17, 0x81C1, 0x0A76, 0x1D18, 0x96AF, 0x33C4, 0xB873, 0xAF1D, 0x24AA,
0x932B, 0x189C, 0x0FF2, 0x8445, 0x212E, 0xAA99, 0xBDF7, 0x3640, 0x7C96, 0xF721, 0xE04F, 0x6BF8, 0xCE93, 0x4524, 0x524A, 0xD9FD,
0xC7E6, 0x4C51, 0x5B3F, 0xD088, 0x75E3, 0xFE54, 0xE93A, 0x628D, 0x285B, 0xA3EC, 0xB482, 0x3F35, 0x9A5E, 0x11E9, 0x0687, 0x8D30,
0xE232, 0x6985, 0x7EEB, 0xF55C, 0x5037, 0xDB80, 0xCCEE, 0x4759, 0x0D8F, 0x8638, 0x9156, 0x1AE1, 0xBF8A, 0x343D, 0x2353, 0xA8E4,
0xB6FF, 0x3D48, 0x2A26, 0xA191, 0x04FA, 0x8F4D, 0x9823, 0x1394, 0x5942, 0xD2F5, 0xC59B, 0x4E2C, 0xEB47, 0x60F0, 0x779E, 0xFC29,
0x4BA8, 0xC01F, 0xD771, 0x5CC6, 0xF9AD, 0x721A, 0x6574, 0xEEC3, 0xA415, 0x2FA2, 0x38CC, 0xB37B, 0x1610, 0x9DA7, 0x8AC9, 0x017E,
0x1F65, 0x94D2, 0x83BC, 0x080B, 0xAD60, 0x26D7, 0x31B9, 0xBA0E, 0xF0D8, 0x7B6F, 0x6C01, 0xE7B6, 0x42DD, 0xC96A, 0xDE04, 0x55B3,
};


void crc16_t10_dif_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x8BB7;
            else
                crc = (crc << 1);
        }
        crc16T10_DIF_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16T10_DIF_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16T10_DIF_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_t10_dif_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16T10_DIF_table[tmp&0xFF];
    }
    return crc; // crc
}
/******************************************************************************
 * Name:    CRC-16/TELEDISK     x16+x15+x13+x7+x4+x2+x+1
 * Poly:    0xA097
 * Init:    0x0000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
static uint16_t crc16TELEDISK_table[256] = {
0x0000, 0xA097, 0xE1B9, 0x412E, 0x63E5, 0xC372, 0x825C, 0x22CB, 0xC7CA, 0x675D, 0x2673, 0x86E4, 0xA42F, 0x04B8, 0x4596, 0xE501,
0x2F03, 0x8F94, 0xCEBA, 0x6E2D, 0x4CE6, 0xEC71, 0xAD5F, 0x0DC8, 0xE8C9, 0x485E, 0x0970, 0xA9E7, 0x8B2C, 0x2BBB, 0x6A95, 0xCA02,
0x5E06, 0xFE91, 0xBFBF, 0x1F28, 0x3DE3, 0x9D74, 0xDC5A, 0x7CCD, 0x99CC, 0x395B, 0x7875, 0xD8E2, 0xFA29, 0x5ABE, 0x1B90, 0xBB07,
0x7105, 0xD192, 0x90BC, 0x302B, 0x12E0, 0xB277, 0xF359, 0x53CE, 0xB6CF, 0x1658, 0x5776, 0xF7E1, 0xD52A, 0x75BD, 0x3493, 0x9404,
0xBC0C, 0x1C9B, 0x5DB5, 0xFD22, 0xDFE9, 0x7F7E, 0x3E50, 0x9EC7, 0x7BC6, 0xDB51, 0x9A7F, 0x3AE8, 0x1823, 0xB8B4, 0xF99A, 0x590D,
0x930F, 0x3398, 0x72B6, 0xD221, 0xF0EA, 0x507D, 0x1153, 0xB1C4, 0x54C5, 0xF452, 0xB57C, 0x15EB, 0x3720, 0x97B7, 0xD699, 0x760E,
0xE20A, 0x429D, 0x03B3, 0xA324, 0x81EF, 0x2178, 0x6056, 0xC0C1, 0x25C0, 0x8557, 0xC479, 0x64EE, 0x4625, 0xE6B2, 0xA79C, 0x070B,
0xCD09, 0x6D9E, 0x2CB0, 0x8C27, 0xAEEC, 0x0E7B, 0x4F55, 0xEFC2, 0x0AC3, 0xAA54, 0xEB7A, 0x4BED, 0x6926, 0xC9B1, 0x889F, 0x2808,
0xD88F, 0x7818, 0x3936, 0x99A1, 0xBB6A, 0x1BFD, 0x5AD3, 0xFA44, 0x1F45, 0xBFD2, 0xFEFC, 0x5E6B, 0x7CA0, 0xDC37, 0x9D19, 0x3D8E,
0xF78C, 0x571B, 0x1635, 0xB6A2, 0x9469, 0x34FE, 0x75D0, 0xD547, 0x3046, 0x90D1, 0xD1FF, 0x7168, 0x53A3, 0xF334, 0xB21A, 0x128D,
0x8689, 0x261E, 0x6730, 0xC7A7, 0xE56C, 0x45FB, 0x04D5, 0xA442, 0x4143, 0xE1D4, 0xA0FA, 0x006D, 0x22A6, 0x8231, 0xC31F, 0x6388,
0xA98A, 0x091D, 0x4833, 0xE8A4, 0xCA6F, 0x6AF8, 0x2BD6, 0x8B41, 0x6E40, 0xCED7, 0x8FF9, 0x2F6E, 0x0DA5, 0xAD32, 0xEC1C, 0x4C8B,
0x6483, 0xC414, 0x853A, 0x25AD, 0x0766, 0xA7F1, 0xE6DF, 0x4648, 0xA349, 0x03DE, 0x42F0, 0xE267, 0xC0AC, 0x603B, 0x2115, 0x8182,
0x4B80, 0xEB17, 0xAA39, 0x0AAE, 0x2865, 0x88F2, 0xC9DC, 0x694B, 0x8C4A, 0x2CDD, 0x6DF3, 0xCD64, 0xEFAF, 0x4F38, 0x0E16, 0xAE81,
0x3A85, 0x9A12, 0xDB3C, 0x7BAB, 0x5960, 0xF9F7, 0xB8D9, 0x184E, 0xFD4F, 0x5DD8, 0x1CF6, 0xBC61, 0x9EAA, 0x3E3D, 0x7F13, 0xDF84,
0x1586, 0xB511, 0xF43F, 0x54A8, 0x7663, 0xD6F4, 0x97DA, 0x374D, 0xD24C, 0x72DB, 0x33F5, 0x9362, 0xB1A9, 0x113E, 0x5010, 0xF087,
};


void crc16_teledisk_GenerateTable()
{
    uint16_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<8;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0xA097;
            else
                crc = (crc << 1);
        }
        crc16TELEDISK_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint16_t crc16TELEDISK_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%04X, ",crc16TELEDISK_table[i]);
//        if( (i+1)%16 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint16_t crc16_teledisk_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 8) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc16TELEDISK_table[tmp&0xFF];
    }
    return crc; // crc
}
/******************************************************************************
 * Name:    CRC-16/TMS37157          x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0x89EC
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
uint16_t crc16_tms37157_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0x3791;                // Initial value    // 0x3791 = reverse 0x89EC
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16CCITT_table[tmp&0xFF];
    }
    return crc; // crc
}
/******************************************************************************
 * Name:    CRC-16/A          x16+x12+x5+1
 * Poly:    0x1021
 * Init:    0xC6C6
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000
 * Note:
 *****************************************************************************/
uint16_t crc16_a_calc(uint8_t *data, uint32_t length)
{
    uint16_t tmp = 0;
    uint16_t crc = 0x6363;                // Initial value    // 0x6363 = reverse 0xC6C6
    while(length--)
    {
        tmp =  (0x00FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc16CCITT_table[tmp&0xFF];
    }
    return crc; // crc
}



/******************************************************************************
 * Name:    CRC-32  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0xFFFFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0xFFFFFFF
 * Alias:   CRC_32/ADCCP
 * Use:     WinRAR,ect.
 *****************************************************************************/
static uint32_t crc32_table[256] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
};

void crc32_GenerateTable()
{
    uint32_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
            else
                crc = (crc >> 1);
        }
        crc32_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint32_t crc32_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%08X, ",crc32_table[i]);
//        if( (i+1)%8 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint32_t crc32_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0xffffffff;                // Initial value
    while(length--)
    {
        tmp =  (0x000000FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc32_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFFFFFF
}

/******************************************************************************
 * Name:    CRC-32  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Note:    d for直接计算
 *****************************************************************************/
uint32_t crc32_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint32_t crc = 0xffffffff;        // Initial value
    while(length--)
    {
        crc ^= *data++;                // crc ^= *data; data++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
            else
                crc = (crc >> 1);
        }
    }
    return ~crc;
}

/******************************************************************************
 * Name:    CRC-32/MPEG-2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0xFFFFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000000
 * Note:
 *****************************************************************************/
static uint32_t crc32MPEG_2_table[256] = {
0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005,
0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61, 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD,
0x4C11DB70, 0x48D0C6C7, 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3, 0x709F7B7A, 0x745E66CD,
0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039, 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5,
0xBE2B5B58, 0xBAEA46EF, 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB, 0xCEB42022, 0xCA753D95,
0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1, 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D,
0x34867077, 0x30476DC0, 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4, 0x0808D07D, 0x0CC9CDCA,
0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE, 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02,
0x5E9F46BF, 0x5A5E5B08, 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC, 0xB6238B25, 0xB2E29692,
0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6, 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A,
0xE0B41DE7, 0xE4750050, 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34, 0xDC3ABDED, 0xD8FBA05A,
0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637, 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB,
0x4F040D56, 0x4BC510E1, 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5, 0x3F9B762C, 0x3B5A6B9B,
0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF, 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623,
0xF12F560E, 0xF5EE4BB9, 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD, 0xCDA1F604, 0xC960EBB3,
0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7, 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B,
0x9B3660C6, 0x9FF77D71, 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2, 0x470CDD2B, 0x43CDC09C,
0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8, 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24,
0x119B4BE9, 0x155A565E, 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A, 0x2D15EBE3, 0x29D4F654,
0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0, 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C,
0xE3A1CBC1, 0xE760D676, 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662, 0x933EB0BB, 0x97FFAD0C,
0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4,
};

void crc32_mpeg_2_GenerateTable()
{
    uint32_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<24;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x80000000)
                crc = (crc << 1) ^ 0x04C11DB7;
            else
                crc = (crc << 1);
        }
        crc32MPEG_2_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint32_t crc32MPEG_2_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%08X, ",crc32MPEG_2_table[i]);
//        if( (i+1)%8 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint32_t crc32_mpeg_2_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0xffffffff;                // Initial value
    while(length--)
    {
        tmp = (crc >> 24) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc32MPEG_2_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-32/MPEG-2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Note:    d for直接计算
 *****************************************************************************/
uint32_t crc32_mpeg_2_d(uint8_t *data, uint32_t length)
{
    uint8_t i;
    uint32_t crc = 0xffffffff;  // Initial value
    while(length--)
    {
        crc ^= (uint32_t)(*data++) << 24;// crc ^=(uint32_t)(*data)<<24; data++;
        for (i = 0; i < 8; ++i)
        {
            if ( crc & 0x80000000 )
                crc = (crc << 1) ^ 0x04C11DB7;
            else
                crc <<= 1;
        }
    }
    return crc;
}


/******************************************************************************
 * Name:    CRC-32/BZIP2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0xFFFFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0xFFFFFFF
 * Note:
 *****************************************************************************/
uint32_t crc32_bzip2_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0xffffffff;                // Initial value
    while(length--)
    {
        tmp = (crc >> 24) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc32MPEG_2_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFFFFFF
}
/******************************************************************************
 * Name:    CRC-32/POSIX  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0x0000000
 * Refin:   False
 * Refout:  False
 * Xorout:  0xFFFFFFF
 * Note:
 *****************************************************************************/
uint32_t crc32_posix_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0x0000000;                // Initial value
    while(length--)
    {
        tmp = (crc >> 24) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc32MPEG_2_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFFFFFF
}
/******************************************************************************
 * Name:    CRC-32  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0xFFFFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0x0000000
 * Alias:   CRC_32/ADCCP
 * Use:     WinRAR,ect.
 *****************************************************************************/
uint32_t crc32_jamcrc_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0xffffffff;                // Initial value
    while(length--)
    {
        tmp =  (0x000000FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc32_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-32/XFER  x32+x7+x5+x3+x3+x+1
 * Poly:    0x000000AF
 * Init:    0x00000000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00000000
 * Note:
 *****************************************************************************/
static uint32_t crc32XFER_table[256] = {
0x00000000, 0x000000AF, 0x0000015E, 0x000001F1, 0x000002BC, 0x00000213, 0x000003E2, 0x0000034D,
0x00000578, 0x000005D7, 0x00000426, 0x00000489, 0x000007C4, 0x0000076B, 0x0000069A, 0x00000635,
0x00000AF0, 0x00000A5F, 0x00000BAE, 0x00000B01, 0x0000084C, 0x000008E3, 0x00000912, 0x000009BD,
0x00000F88, 0x00000F27, 0x00000ED6, 0x00000E79, 0x00000D34, 0x00000D9B, 0x00000C6A, 0x00000CC5,
0x000015E0, 0x0000154F, 0x000014BE, 0x00001411, 0x0000175C, 0x000017F3, 0x00001602, 0x000016AD,
0x00001098, 0x00001037, 0x000011C6, 0x00001169, 0x00001224, 0x0000128B, 0x0000137A, 0x000013D5,
0x00001F10, 0x00001FBF, 0x00001E4E, 0x00001EE1, 0x00001DAC, 0x00001D03, 0x00001CF2, 0x00001C5D,
0x00001A68, 0x00001AC7, 0x00001B36, 0x00001B99, 0x000018D4, 0x0000187B, 0x0000198A, 0x00001925,
0x00002BC0, 0x00002B6F, 0x00002A9E, 0x00002A31, 0x0000297C, 0x000029D3, 0x00002822, 0x0000288D,
0x00002EB8, 0x00002E17, 0x00002FE6, 0x00002F49, 0x00002C04, 0x00002CAB, 0x00002D5A, 0x00002DF5,
0x00002130, 0x0000219F, 0x0000206E, 0x000020C1, 0x0000238C, 0x00002323, 0x000022D2, 0x0000227D,
0x00002448, 0x000024E7, 0x00002516, 0x000025B9, 0x000026F4, 0x0000265B, 0x000027AA, 0x00002705,
0x00003E20, 0x00003E8F, 0x00003F7E, 0x00003FD1, 0x00003C9C, 0x00003C33, 0x00003DC2, 0x00003D6D,
0x00003B58, 0x00003BF7, 0x00003A06, 0x00003AA9, 0x000039E4, 0x0000394B, 0x000038BA, 0x00003815,
0x000034D0, 0x0000347F, 0x0000358E, 0x00003521, 0x0000366C, 0x000036C3, 0x00003732, 0x0000379D,
0x000031A8, 0x00003107, 0x000030F6, 0x00003059, 0x00003314, 0x000033BB, 0x0000324A, 0x000032E5,
0x00005780, 0x0000572F, 0x000056DE, 0x00005671, 0x0000553C, 0x00005593, 0x00005462, 0x000054CD,
0x000052F8, 0x00005257, 0x000053A6, 0x00005309, 0x00005044, 0x000050EB, 0x0000511A, 0x000051B5,
0x00005D70, 0x00005DDF, 0x00005C2E, 0x00005C81, 0x00005FCC, 0x00005F63, 0x00005E92, 0x00005E3D,
0x00005808, 0x000058A7, 0x00005956, 0x000059F9, 0x00005AB4, 0x00005A1B, 0x00005BEA, 0x00005B45,
0x00004260, 0x000042CF, 0x0000433E, 0x00004391, 0x000040DC, 0x00004073, 0x00004182, 0x0000412D,
0x00004718, 0x000047B7, 0x00004646, 0x000046E9, 0x000045A4, 0x0000450B, 0x000044FA, 0x00004455,
0x00004890, 0x0000483F, 0x000049CE, 0x00004961, 0x00004A2C, 0x00004A83, 0x00004B72, 0x00004BDD,
0x00004DE8, 0x00004D47, 0x00004CB6, 0x00004C19, 0x00004F54, 0x00004FFB, 0x00004E0A, 0x00004EA5,
0x00007C40, 0x00007CEF, 0x00007D1E, 0x00007DB1, 0x00007EFC, 0x00007E53, 0x00007FA2, 0x00007F0D,
0x00007938, 0x00007997, 0x00007866, 0x000078C9, 0x00007B84, 0x00007B2B, 0x00007ADA, 0x00007A75,
0x000076B0, 0x0000761F, 0x000077EE, 0x00007741, 0x0000740C, 0x000074A3, 0x00007552, 0x000075FD,
0x000073C8, 0x00007367, 0x00007296, 0x00007239, 0x00007174, 0x000071DB, 0x0000702A, 0x00007085,
0x000069A0, 0x0000690F, 0x000068FE, 0x00006851, 0x00006B1C, 0x00006BB3, 0x00006A42, 0x00006AED,
0x00006CD8, 0x00006C77, 0x00006D86, 0x00006D29, 0x00006E64, 0x00006ECB, 0x00006F3A, 0x00006F95,
0x00006350, 0x000063FF, 0x0000620E, 0x000062A1, 0x000061EC, 0x00006143, 0x000060B2, 0x0000601D,
0x00006628, 0x00006687, 0x00006776, 0x000067D9, 0x00006494, 0x0000643B, 0x000065CA, 0x00006565,
};

void crc32_xfer_GenerateTable()
{
    uint32_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<24;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x80000000)
                crc = (crc << 1) ^ 0x000000AF;
            else
                crc = (crc << 1);
        }
        crc32XFER_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint32_t crc32XFER_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%08X, ",crc32XFER_table[i]);
//        if( (i+1)%8 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint32_t crc32_xfer_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 24) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc32XFER_table[tmp&0xFF];
    }
    return crc; // crc
}

/******************************************************************************
 * Name:    CRC-32/C
 * Poly:    0x1EDC6F41
 * Init:    0xFFFFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0xFFFFFFF
 *****************************************************************************/
static uint32_t crc32C_table[256] = {
0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4, 0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB,
0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B, 0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24,
0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B, 0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384,
0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54, 0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B,
0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A, 0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35,
0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5, 0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA,
0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45, 0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A,
0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A, 0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595,
0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48, 0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957,
0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687, 0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198,
0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927, 0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38,
0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8, 0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7,
0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096, 0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789,
0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859, 0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46,
0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9, 0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6,
0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36, 0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829,
0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C, 0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93,
0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043, 0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C,
0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3, 0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC,
0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C, 0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033,
0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652, 0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D,
0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D, 0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982,
0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D, 0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622,
0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2, 0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED,
0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530, 0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F,
0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF, 0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0,
0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F, 0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540,
0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90, 0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F,
0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE, 0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1,
0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321, 0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E,
0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81, 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E,
0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E, 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351,
};

void crc32_c_GenerateTable()
{
    uint32_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x82F63B78;// 0x82F63B78 = reverse 0x1EDC6F41
            else
                crc = (crc >> 1);
        }
        crc32C_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint32_t crc32C_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%08X, ",crc32C_table[i]);
//        if( (i+1)%8 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint32_t crc32_c_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0xffffffff;                // Initial value
    while(length--)
    {
        tmp =  (0x000000FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc32C_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFFFFFF
}

/******************************************************************************
 * Name:    CRC-32/D
 * Poly:    0xA833982B
 * Init:    0xFFFFFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0xFFFFFFFF
 *****************************************************************************/
static uint32_t crc32D_table[256] = {
0x00000000, 0x2BDDD04F, 0x57BBA09E, 0x7C6670D1, 0xAF77413C, 0x84AA9173, 0xF8CCE1A2, 0xD31131ED,
0xF6DD1A53, 0xDD00CA1C, 0xA166BACD, 0x8ABB6A82, 0x59AA5B6F, 0x72778B20, 0x0E11FBF1, 0x25CC2BBE,
0x4589AC8D, 0x6E547CC2, 0x12320C13, 0x39EFDC5C, 0xEAFEEDB1, 0xC1233DFE, 0xBD454D2F, 0x96989D60,
0xB354B6DE, 0x98896691, 0xE4EF1640, 0xCF32C60F, 0x1C23F7E2, 0x37FE27AD, 0x4B98577C, 0x60458733,
0x8B13591A, 0xA0CE8955, 0xDCA8F984, 0xF77529CB, 0x24641826, 0x0FB9C869, 0x73DFB8B8, 0x580268F7,
0x7DCE4349, 0x56139306, 0x2A75E3D7, 0x01A83398, 0xD2B90275, 0xF964D23A, 0x8502A2EB, 0xAEDF72A4,
0xCE9AF597, 0xE54725D8, 0x99215509, 0xB2FC8546, 0x61EDB4AB, 0x4A3064E4, 0x36561435, 0x1D8BC47A,
0x3847EFC4, 0x139A3F8B, 0x6FFC4F5A, 0x44219F15, 0x9730AEF8, 0xBCED7EB7, 0xC08B0E66, 0xEB56DE29,
0xBE152A1F, 0x95C8FA50, 0xE9AE8A81, 0xC2735ACE, 0x11626B23, 0x3ABFBB6C, 0x46D9CBBD, 0x6D041BF2,
0x48C8304C, 0x6315E003, 0x1F7390D2, 0x34AE409D, 0xE7BF7170, 0xCC62A13F, 0xB004D1EE, 0x9BD901A1,
0xFB9C8692, 0xD04156DD, 0xAC27260C, 0x87FAF643, 0x54EBC7AE, 0x7F3617E1, 0x03506730, 0x288DB77F,
0x0D419CC1, 0x269C4C8E, 0x5AFA3C5F, 0x7127EC10, 0xA236DDFD, 0x89EB0DB2, 0xF58D7D63, 0xDE50AD2C,
0x35067305, 0x1EDBA34A, 0x62BDD39B, 0x496003D4, 0x9A713239, 0xB1ACE276, 0xCDCA92A7, 0xE61742E8,
0xC3DB6956, 0xE806B919, 0x9460C9C8, 0xBFBD1987, 0x6CAC286A, 0x4771F825, 0x3B1788F4, 0x10CA58BB,
0x708FDF88, 0x5B520FC7, 0x27347F16, 0x0CE9AF59, 0xDFF89EB4, 0xF4254EFB, 0x88433E2A, 0xA39EEE65,
0x8652C5DB, 0xAD8F1594, 0xD1E96545, 0xFA34B50A, 0x292584E7, 0x02F854A8, 0x7E9E2479, 0x5543F436,
0xD419CC15, 0xFFC41C5A, 0x83A26C8B, 0xA87FBCC4, 0x7B6E8D29, 0x50B35D66, 0x2CD52DB7, 0x0708FDF8,
0x22C4D646, 0x09190609, 0x757F76D8, 0x5EA2A697, 0x8DB3977A, 0xA66E4735, 0xDA0837E4, 0xF1D5E7AB,
0x91906098, 0xBA4DB0D7, 0xC62BC006, 0xEDF61049, 0x3EE721A4, 0x153AF1EB, 0x695C813A, 0x42815175,
0x674D7ACB, 0x4C90AA84, 0x30F6DA55, 0x1B2B0A1A, 0xC83A3BF7, 0xE3E7EBB8, 0x9F819B69, 0xB45C4B26,
0x5F0A950F, 0x74D74540, 0x08B13591, 0x236CE5DE, 0xF07DD433, 0xDBA0047C, 0xA7C674AD, 0x8C1BA4E2,
0xA9D78F5C, 0x820A5F13, 0xFE6C2FC2, 0xD5B1FF8D, 0x06A0CE60, 0x2D7D1E2F, 0x511B6EFE, 0x7AC6BEB1,
0x1A833982, 0x315EE9CD, 0x4D38991C, 0x66E54953, 0xB5F478BE, 0x9E29A8F1, 0xE24FD820, 0xC992086F,
0xEC5E23D1, 0xC783F39E, 0xBBE5834F, 0x90385300, 0x432962ED, 0x68F4B2A2, 0x1492C273, 0x3F4F123C,
0x6A0CE60A, 0x41D13645, 0x3DB74694, 0x166A96DB, 0xC57BA736, 0xEEA67779, 0x92C007A8, 0xB91DD7E7,
0x9CD1FC59, 0xB70C2C16, 0xCB6A5CC7, 0xE0B78C88, 0x33A6BD65, 0x187B6D2A, 0x641D1DFB, 0x4FC0CDB4,
0x2F854A87, 0x04589AC8, 0x783EEA19, 0x53E33A56, 0x80F20BBB, 0xAB2FDBF4, 0xD749AB25, 0xFC947B6A,
0xD95850D4, 0xF285809B, 0x8EE3F04A, 0xA53E2005, 0x762F11E8, 0x5DF2C1A7, 0x2194B176, 0x0A496139,
0xE11FBF10, 0xCAC26F5F, 0xB6A41F8E, 0x9D79CFC1, 0x4E68FE2C, 0x65B52E63, 0x19D35EB2, 0x320E8EFD,
0x17C2A543, 0x3C1F750C, 0x407905DD, 0x6BA4D592, 0xB8B5E47F, 0x93683430, 0xEF0E44E1, 0xC4D394AE,
0xA496139D, 0x8F4BC3D2, 0xF32DB303, 0xD8F0634C, 0x0BE152A1, 0x203C82EE, 0x5C5AF23F, 0x77872270,
0x524B09CE, 0x7996D981, 0x05F0A950, 0x2E2D791F, 0xFD3C48F2, 0xD6E198BD, 0xAA87E86C, 0x815A3823,
};

void crc32_d_GenerateTable()
{
    uint32_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0xD419CC15;// 0x82F63B78 = reverse 0xA833982B
            else
                crc = (crc >> 1);
        }
        crc32D_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint32_t crc32D_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%08X, ",crc32D_table[i]);
//        if( (i+1)%8 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint32_t crc32_d_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0xffffffff;                // Initial value
    while(length--)
    {
        tmp =  (0x000000FF & crc) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc >> 8) ^ crc32D_table[tmp&0xFF];
    }
    return ~crc; // crc ^ 0xFFFFFFFF
}

/******************************************************************************
 * Name:    CRC-32/Q
 * Poly:    0x814141AB
 * Init:    0x00000000
 * Refin:   False
 * Refout:  False
 * Xorout:  0x00000000
 * Note:
 *****************************************************************************/
static uint32_t crc32Q_table[256] = {
0x00000000, 0x814141AB, 0x83C3C2FD, 0x02828356, 0x86C6C451, 0x078785FA, 0x050506AC, 0x84444707,
0x8CCCC909, 0x0D8D88A2, 0x0F0F0BF4, 0x8E4E4A5F, 0x0A0A0D58, 0x8B4B4CF3, 0x89C9CFA5, 0x08888E0E,
0x98D8D3B9, 0x19999212, 0x1B1B1144, 0x9A5A50EF, 0x1E1E17E8, 0x9F5F5643, 0x9DDDD515, 0x1C9C94BE,
0x14141AB0, 0x95555B1B, 0x97D7D84D, 0x169699E6, 0x92D2DEE1, 0x13939F4A, 0x11111C1C, 0x90505DB7,
0xB0F0E6D9, 0x31B1A772, 0x33332424, 0xB272658F, 0x36362288, 0xB7776323, 0xB5F5E075, 0x34B4A1DE,
0x3C3C2FD0, 0xBD7D6E7B, 0xBFFFED2D, 0x3EBEAC86, 0xBAFAEB81, 0x3BBBAA2A, 0x3939297C, 0xB87868D7,
0x28283560, 0xA96974CB, 0xABEBF79D, 0x2AAAB636, 0xAEEEF131, 0x2FAFB09A, 0x2D2D33CC, 0xAC6C7267,
0xA4E4FC69, 0x25A5BDC2, 0x27273E94, 0xA6667F3F, 0x22223838, 0xA3637993, 0xA1E1FAC5, 0x20A0BB6E,
0xE0A08C19, 0x61E1CDB2, 0x63634EE4, 0xE2220F4F, 0x66664848, 0xE72709E3, 0xE5A58AB5, 0x64E4CB1E,
0x6C6C4510, 0xED2D04BB, 0xEFAF87ED, 0x6EEEC646, 0xEAAA8141, 0x6BEBC0EA, 0x696943BC, 0xE8280217,
0x78785FA0, 0xF9391E0B, 0xFBBB9D5D, 0x7AFADCF6, 0xFEBE9BF1, 0x7FFFDA5A, 0x7D7D590C, 0xFC3C18A7,
0xF4B496A9, 0x75F5D702, 0x77775454, 0xF63615FF, 0x727252F8, 0xF3331353, 0xF1B19005, 0x70F0D1AE,
0x50506AC0, 0xD1112B6B, 0xD393A83D, 0x52D2E996, 0xD696AE91, 0x57D7EF3A, 0x55556C6C, 0xD4142DC7,
0xDC9CA3C9, 0x5DDDE262, 0x5F5F6134, 0xDE1E209F, 0x5A5A6798, 0xDB1B2633, 0xD999A565, 0x58D8E4CE,
0xC888B979, 0x49C9F8D2, 0x4B4B7B84, 0xCA0A3A2F, 0x4E4E7D28, 0xCF0F3C83, 0xCD8DBFD5, 0x4CCCFE7E,
0x44447070, 0xC50531DB, 0xC787B28D, 0x46C6F326, 0xC282B421, 0x43C3F58A, 0x414176DC, 0xC0003777,
0x40005999, 0xC1411832, 0xC3C39B64, 0x4282DACF, 0xC6C69DC8, 0x4787DC63, 0x45055F35, 0xC4441E9E,
0xCCCC9090, 0x4D8DD13B, 0x4F0F526D, 0xCE4E13C6, 0x4A0A54C1, 0xCB4B156A, 0xC9C9963C, 0x4888D797,
0xD8D88A20, 0x5999CB8B, 0x5B1B48DD, 0xDA5A0976, 0x5E1E4E71, 0xDF5F0FDA, 0xDDDD8C8C, 0x5C9CCD27,
0x54144329, 0xD5550282, 0xD7D781D4, 0x5696C07F, 0xD2D28778, 0x5393C6D3, 0x51114585, 0xD050042E,
0xF0F0BF40, 0x71B1FEEB, 0x73337DBD, 0xF2723C16, 0x76367B11, 0xF7773ABA, 0xF5F5B9EC, 0x74B4F847,
0x7C3C7649, 0xFD7D37E2, 0xFFFFB4B4, 0x7EBEF51F, 0xFAFAB218, 0x7BBBF3B3, 0x793970E5, 0xF878314E,
0x68286CF9, 0xE9692D52, 0xEBEBAE04, 0x6AAAEFAF, 0xEEEEA8A8, 0x6FAFE903, 0x6D2D6A55, 0xEC6C2BFE,
0xE4E4A5F0, 0x65A5E45B, 0x6727670D, 0xE66626A6, 0x622261A1, 0xE363200A, 0xE1E1A35C, 0x60A0E2F7,
0xA0A0D580, 0x21E1942B, 0x2363177D, 0xA22256D6, 0x266611D1, 0xA727507A, 0xA5A5D32C, 0x24E49287,
0x2C6C1C89, 0xAD2D5D22, 0xAFAFDE74, 0x2EEE9FDF, 0xAAAAD8D8, 0x2BEB9973, 0x29691A25, 0xA8285B8E,
0x38780639, 0xB9394792, 0xBBBBC4C4, 0x3AFA856F, 0xBEBEC268, 0x3FFF83C3, 0x3D7D0095, 0xBC3C413E,
0xB4B4CF30, 0x35F58E9B, 0x37770DCD, 0xB6364C66, 0x32720B61, 0xB3334ACA, 0xB1B1C99C, 0x30F08837,
0x10503359, 0x911172F2, 0x9393F1A4, 0x12D2B00F, 0x9696F708, 0x17D7B6A3, 0x155535F5, 0x9414745E,
0x9C9CFA50, 0x1DDDBBFB, 0x1F5F38AD, 0x9E1E7906, 0x1A5A3E01, 0x9B1B7FAA, 0x9999FCFC, 0x18D8BD57,
0x8888E0E0, 0x09C9A14B, 0x0B4B221D, 0x8A0A63B6, 0x0E4E24B1, 0x8F0F651A, 0x8D8DE64C, 0x0CCCA7E7,
0x044429E9, 0x85056842, 0x8787EB14, 0x06C6AABF, 0x8282EDB8, 0x03C3AC13, 0x01412F45, 0x80006EEE,
};

void crc32_q_GenerateTable()
{
    uint32_t crc = 0;
    for(uint16_t i=0; i<256; i++){
        crc = i<<24;               // Initial value
        for (uint16_t j = 0; j < 8; ++j){
            if (crc & 0x80000000)
                crc = (crc << 1) ^ 0x814141AB;
            else
                crc = (crc << 1);
        }
        crc32Q_table[i] = crc;
    }
    /* 用于打印数组 */
//    printf("static uint32_t crc32Q_table[256] = {\n");
//    for(uint16_t i=0; i<256; i++){
//        printf("0x%08X, ",crc32Q_table[i]);
//        if( (i+1)%8 == 0 ){
//            printf("\n");
//        }
//    }
//    printf("};\n");
}

uint32_t crc32_q_calc(uint8_t *data, uint32_t length)
{
    uint32_t tmp = 0;
    uint32_t crc = 0;                // Initial value
    while(length--)
    {
        tmp = (crc >> 24) ^ (*data++);    // crc ^= *data; data++;
        crc = (crc << 8) ^ crc32Q_table[tmp&0xFF];
    }
    return crc; // crc
}


